[英]Mongoose embedded document query returning null
I have the following schema : 我有以下架构:
_schema : {
Prize : new Schema({
prizeName : { type : String },
thumbnailImage : [ String ],
detailImage : [ String ],
prizeCategory : [ {type : String, index : true } ],
prizeDescription : { type : String },
prizePrice : { type : Number, required : true }
}),
Game : new Schema ({
roomName : { type : String, required : true },
openTime : { type : Date },
closeTime : { type : Date },
minPlayers : { type : Number },
maxPlayers : { type : Number, required : true },
numberOfPlayers : { type : Number },
winner : { userId : { type : ObjectId, index : true, ref : 'User'} },
prize : [ this.Prize ],
tag : [ { type : String, index : true } ],
status : { type : Number, index : true },
businessType : { type : Number, required : true, index : true },
mallId : { type : ObjectId, ref : 'Mall' },
registeredPlayers : { type : ObjectId, ref : 'User' }
}),
Schedule : new Schema ({
_id : ObjectId,
time : { type : Date, index : true },
game : [ this.Game ]
}),
}
However when I try to query the game embedded document the object is always null. 但是,当我尝试查询游戏嵌入文档时,该对象始终为null。 I'm querying like so: 我这样查询:
var Schedule = mongoose.model('Schedule', this._schema.Schedule);
Schedule.findById({'game._id' : req.params._id}).exec(function(err,gameDetail){...});
Am I doing anything wrong when I declare the schema and models? 当我声明架构和模型时,我做错了吗? I have seen numerous examples where people appear to be doing exactly what I'm trying. 我看过很多例子,人们似乎正在做我正在尝试的事情。 Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks in advance. 提前致谢。
A mongoose Model's findById
method is used to find the instance of that Model with the _id
that's supplied as the first parameter to the method. findById
Model的findById
方法用于查找该模型的实例,其中_id
是作为方法的第一个参数提供的。 So Schedule.findById
returns Schedule
instances, not individual Game
instances. 因此, Schedule.findById
返回Schedule
实例,而不是单个Game
实例。 Schedule.findOne({'game._id' : req.params._id}, ...
will get you the Schedule
instance containing the Game
with that id, but if you need to query for Game
instances by id, you should be keeping them in a separate collection instead of embedding them in Schedule
. Schedule.findOne({'game._id' : req.params._id}, ...
将为您提供包含具有该ID的Game
的Schedule
实例,但是如果您需要通过id查询Game
实例,则应该保留它们在一个单独的集合中,而不是将它们嵌入到Schedule
。
Looking at your code, my first guess is actually that your findById
isn't structured quite right. 看看你的代码,我的第一个猜测实际上是你的findById
结构不正确。
First, you're using the value req.params._id
to get the id. 首先,您使用值req.params._id
来获取ID。 Most of the code examples I have seen, and my code, uses :id
in the router ( app.get('/schedules/:id')
), which would actually mean the ID you're looking for is stored in req.params.id
. 我见过的大多数代码示例和我的代码都使用:id
路由器中的:id
( app.get('/schedules/:id')
),这实际上意味着您要查找的ID存储在req.params.id
。 You'll have to check that in your code. 你必须在你的代码中检查它。
Secondly, to my understanding, findById is only useful for finding, in this case, a Schedule by that ID. 其次,根据我的理解,findById仅用于在这种情况下通过该ID查找Schedule。 Try using just a normal find. 尝试使用普通的查找。
Last thought: you're missing a closing curly bracket at the end of {'game._id' : req.params._id}
. 最后一想:你在{'game._id' : req.params._id}
的末尾错过了一个结束的花括号。
Hopefully something in that helps. 希望有所帮助。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.