Node.js 猫鼬填充法

Node.js 猫鼬填充法,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个comments集合,其中包含\u id的自定义值,而不是mongo objectid: var exports = module.exports = CommentsSchema = new mongoose.Schema({}); CommentsSchema.add({ _id : {type : Number, required : true, index: true, unique : true }, label: {type: String, index: true

我有一个
comments
集合,其中包含
\u id
的自定义值,而不是mongo objectid:

var exports = module.exports = CommentsSchema = new mongoose.Schema({});
CommentsSchema.add({
_id : {type : Number, required : true, index: true, unique : true },
label:      {type: String, index: true, unique : true }//Should be unique
});
记录:

db.comments.insert{_id:1, label : 'some text'});
db.comments.insert({_id:2, label : 'some text'});
另一个模式
发布
,它引用了
注释
,如下所示

PostsSchema.add({
    comments : [{type : Number, ref: 'Comments' }],
});
我尝试使用
mongoose.populate
方法获取评论数据和帖子数据。但由于我有评论的自定义ID,所以我无法在帖子中获取评论数据

Posts.find().populate('Comments').exec(function(err, posts){
console.log(posts);
});
我选中了,它定义了多个选项,这些选项可以传递给populate,但无法使其正常工作。是否可以对自定义的“\u id”或自定义字段使用
填充
方法

最终解决方案 使用如上所述的
CommentsSchema
postschema
,我使用下面的代码得到了这个结果

Posts.find({}).populate('comments').exec(function(err, posts){
    console.log(posts);
    });

从您的代码来看,您似乎没有注释的自定义id
更新了带有_id字段的“注释”模式的问题。我尝试将其添加到模式中,但仍然不起作用。如果您有一个数字I'd,为什么要将ref类型设为string抱歉,忘了在此处更新posts模式。我尝试了数字和字符串,但我无法实现。似乎应该使用populate('comments')—小写和ref类型的数字。“填充”将字段的名称作为参数R