Node.js Can';不要得到猫鼬';填充';工作
我一直在一个简单的mongoose(3.8.8)查询中填充ref字段。 这可能是一个愚蠢的问题,但我实际上无法解决Node.js Can';不要得到猫鼬';填充';工作,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我一直在一个简单的mongoose(3.8.8)查询中填充ref字段。 这可能是一个愚蠢的问题,但我实际上无法解决 var Schema = mongoose.Schema; var ItemSchema = new Schema({ description: { type: String }, comments: [ { type:Schema.Types.ObjectId, ref:'Comment'} ], created: { type: Date, defau
var Schema = mongoose.Schema;
var ItemSchema = new Schema({
description: { type: String },
comments: [ { type:Schema.Types.ObjectId, ref:'Comment'} ],
created: { type: Date, default: Date.now }
});
var Item = mongoose.model('Item', ItemSchema);
var CommentSchema = new Schema({
text: { type: String },
item_id: { type:Schema.Types.ObjectId, ref:'Item'} ,
created: { type: Date, default: Date.now }
});
var Comment = mongoose.model('Comment', CommentSchema);
Item.find().populate('comments').exec(function(err,item){
console.log('Item ',item)
})
数据如下:
/* item0 */
{
"_id" : ObjectId("53da00cc5ddd29442463e716"),
"description" : "item1",
"created" : ISODate("2014-07-31T08:39:40.475Z"),
"comments" : [],
"__v" : 0
}
/* item1 */
{
"_id" : ObjectId("53da00cc5ddd29442463e717"),
"description" : "item2",
"created" : ISODate("2014-07-31T08:39:40.478Z"),
"comments" : [],
"__v" : 0
}
/* comment0 */
{
"_id" : ObjectId("53da01e9ef4ecaa0231fdc8d"),
"item_id" : ObjectId("53da00cc5ddd29442463e716"),
"text" : "comment1",
"created" : ISODate("2014-07-31T08:44:25.768Z"),
"__v" : 0
}
它只是无法按预期工作,无法填充“comments”数组,因为在我的comments集合中,项的id已正确填充。
有什么问题吗
提前感谢数据是什么样子的?很可能有些事情不对劲。因此,可能是注释数组中的项与注释集合中的项不匹配。通常是因为没有正确保存内容。编辑包含数据的帖子您的评论数组在任何情况下都是空的?你期望做什么?它仅基于从创建的注释“推”到数组中的
\u id
值“填充”或“伪联接”。很明显,真正的问题在于代码中你“创建”了注释,但没有做到这一点。我当时是对的,这实际上是一个愚蠢的问题。非常感谢你!