最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Mongoose在按

IT培训 admin 7浏览 0评论

Mongoose在按

我是MongoDB / Mongoose的新手,并且最近使用MongoDB,Node和Express开始了一个项目,我遇到了一个问题,当尝试在存在的_id上使用mongoose进行查询时,结果为null。我发现了类似的问题,但大多数人说mongoose正在查询复数的集合名称,我已经确认这不是我的问题,因为打开mongoose的调试并验证集合是否正确命名。

我的文档存在于报告数据库和客户集合中,当前该集合中只有一个文档。

{
    "_id" : ObjectId("5c64881002ea07789e9444aa"),
    "fields" : {
        "Customer ID" : "_id",
        "First Name" : "first_name",
        "Last Name" : "last_name",
        "Contact Type" : "opt_history.contact_type",
        "Opt In Date" : "opt_history.opt_in_date",
        "Opt Out Date" : "opt_history.opt_out_date",
        "Location Opt In" : "opt_history.location_opt_in",
        "Location Opt Out" : "opt_history.location_opt_out"
    },
    "filters" : {
        "company" : {
            "validation" : {
                "required" : true
            },
            "query_value" : "company:%company_id%"
        },
        "opt_history" : {
            "validation" : {
                "required" : true
            },
            "query_value" : {
                "opt_in" : {
                    "if" : {
                        "start_date" : {
                            "if" : {
                                "end_date" : "{$and: [{\"opt_history.opt_in_date\":{$gte: %start_date%, $lte: %end_date%}}, {\"opt_history.opt_out_date\":{$eq: null}}]}"
                            },
                            "else" : "{$and: [{\"opt_history.opt_in_date\":{$eq: %start_date%}}, {\"opt_history.opt_out_date\":{$eq: null}}]}"
                        }
                    },
                    "else" : "{$and: [{\"opt_history.opt_in_date\":{$ne: null}}, {\"opt_history.opt_out_date\":{$eq: null}}]}"
                },
                "opt_out" : {
                    "if" : {
                        "start_date" : {
                            "if" : {
                                "end_date" : "opt_history.opt_out_date:{$gte: %start_date%, $lte: %end_date%}"
                            },
                            "else" : "opt_history.opt_out_date:{$eq: %start_date%}"
                        }
                    },
                    "else" : "opt_history.opt_out_date:{$ne: null}"
                },
                "no_opt" : "{$or: [{\"opt_history\":null}, {\"opt_history.opt_out_date\":{$nin:[null]}}]}"
            }
        }
    }
}

用于获取文档的代码如下(FWIW我已经尝试了字符串_id和ObjectId,结果是相同的):

exports.run = function(req, res, next) {
    console.log(req.query.id);
    const report = require('../models/Report');
    report.findById("5c64881002ea07789e9444aa").exec(function(err, result) {
        console.log(err);
        console.log(result);
        res.send(result);
    });
};

我打开了mongoose的调试,可以看到正在形成的查询是customer.findOne({ _id: ObjectId("5c64881002ea07789e9444aa") }, { projection: {} }),我试图直接在mongodb中运行该查询并得到以下错误。

db.customer.findOne({ _id: ObjectId("5c64881002ea07789e9444aa") }, { projection: {} })
2019-02-15T11:27:51.425-0700 E QUERY    [js] Error: error: {
    "ok" : 0,
    "errmsg" : ">1 field in obj: {}",
    "code" : 2,
    "codeName" : "BadValue"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCommandCursor@src/mongo/shell/query.js:708:1
DBQuery.prototype._exec@src/mongo/shell/query.js:113:28
DBQuery.prototype.hasNext@src/mongo/shell/query.js:288:5
DBCollection.prototype.findOne@src/mongo/shell/collection.js:260:10
@(shell):1:1

似乎问题在于“投影:{}”,猫鼬似乎注入了我的查询。当我删除该块时,查询将在mongodb中正常运行。所以问题是,为什么mongoose这样做,我该怎么做才能抑制它呢?我想要整个文档,所以我不需要投影任何字段。

回答如下:

我想通了,事实证明我试图在我的猫鼬连接上不正确地切换dbs,所以我搜索的文件从未真正存在过,因为我在错误的数据库中搜索(当时两个数据库都有一个同名的集合但那是在改变)。

Mongoose在按

我是MongoDB / Mongoose的新手,并且最近使用MongoDB,Node和Express开始了一个项目,我遇到了一个问题,当尝试在存在的_id上使用mongoose进行查询时,结果为null。我发现了类似的问题,但大多数人说mongoose正在查询复数的集合名称,我已经确认这不是我的问题,因为打开mongoose的调试并验证集合是否正确命名。

我的文档存在于报告数据库和客户集合中,当前该集合中只有一个文档。

{
    "_id" : ObjectId("5c64881002ea07789e9444aa"),
    "fields" : {
        "Customer ID" : "_id",
        "First Name" : "first_name",
        "Last Name" : "last_name",
        "Contact Type" : "opt_history.contact_type",
        "Opt In Date" : "opt_history.opt_in_date",
        "Opt Out Date" : "opt_history.opt_out_date",
        "Location Opt In" : "opt_history.location_opt_in",
        "Location Opt Out" : "opt_history.location_opt_out"
    },
    "filters" : {
        "company" : {
            "validation" : {
                "required" : true
            },
            "query_value" : "company:%company_id%"
        },
        "opt_history" : {
            "validation" : {
                "required" : true
            },
            "query_value" : {
                "opt_in" : {
                    "if" : {
                        "start_date" : {
                            "if" : {
                                "end_date" : "{$and: [{\"opt_history.opt_in_date\":{$gte: %start_date%, $lte: %end_date%}}, {\"opt_history.opt_out_date\":{$eq: null}}]}"
                            },
                            "else" : "{$and: [{\"opt_history.opt_in_date\":{$eq: %start_date%}}, {\"opt_history.opt_out_date\":{$eq: null}}]}"
                        }
                    },
                    "else" : "{$and: [{\"opt_history.opt_in_date\":{$ne: null}}, {\"opt_history.opt_out_date\":{$eq: null}}]}"
                },
                "opt_out" : {
                    "if" : {
                        "start_date" : {
                            "if" : {
                                "end_date" : "opt_history.opt_out_date:{$gte: %start_date%, $lte: %end_date%}"
                            },
                            "else" : "opt_history.opt_out_date:{$eq: %start_date%}"
                        }
                    },
                    "else" : "opt_history.opt_out_date:{$ne: null}"
                },
                "no_opt" : "{$or: [{\"opt_history\":null}, {\"opt_history.opt_out_date\":{$nin:[null]}}]}"
            }
        }
    }
}

用于获取文档的代码如下(FWIW我已经尝试了字符串_id和ObjectId,结果是相同的):

exports.run = function(req, res, next) {
    console.log(req.query.id);
    const report = require('../models/Report');
    report.findById("5c64881002ea07789e9444aa").exec(function(err, result) {
        console.log(err);
        console.log(result);
        res.send(result);
    });
};

我打开了mongoose的调试,可以看到正在形成的查询是customer.findOne({ _id: ObjectId("5c64881002ea07789e9444aa") }, { projection: {} }),我试图直接在mongodb中运行该查询并得到以下错误。

db.customer.findOne({ _id: ObjectId("5c64881002ea07789e9444aa") }, { projection: {} })
2019-02-15T11:27:51.425-0700 E QUERY    [js] Error: error: {
    "ok" : 0,
    "errmsg" : ">1 field in obj: {}",
    "code" : 2,
    "codeName" : "BadValue"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCommandCursor@src/mongo/shell/query.js:708:1
DBQuery.prototype._exec@src/mongo/shell/query.js:113:28
DBQuery.prototype.hasNext@src/mongo/shell/query.js:288:5
DBCollection.prototype.findOne@src/mongo/shell/collection.js:260:10
@(shell):1:1

似乎问题在于“投影:{}”,猫鼬似乎注入了我的查询。当我删除该块时,查询将在mongodb中正常运行。所以问题是,为什么mongoose这样做,我该怎么做才能抑制它呢?我想要整个文档,所以我不需要投影任何字段。

回答如下:

我想通了,事实证明我试图在我的猫鼬连接上不正确地切换dbs,所以我搜索的文件从未真正存在过,因为我在错误的数据库中搜索(当时两个数据库都有一个同名的集合但那是在改变)。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论