Mongoose:如何在 populate 过程中排除虚拟 id 字段

4

我想要排除Mongoose设置的虚拟id字段。

var Bar = new Schema({ body: String });    
var Foo = new Schema({ bars: type: Schema.Types.ObjectId, ref: 'Bar' });

Foo
.find({..query...})
.populate('bars', 'body -_id')
.exec(function(err, foos){
   console.log(foos); // { bars: [{id: null, body: 'body string'}] }
}); 

我该如何摆脱那个“id”字段?由于在populate中排除了_id,因此它已经是null了。


'body -_id -id' 有效吗? - royhowie
2
没有移除_id,但将id分配为null。 - elloworld111
WiMantis的解决方案对我有效! - Zitoun
1个回答

2

您可以在模式选项中禁用虚拟的id字段(请参阅此处的文档)。在您的情况下,它应该是这样的:

var Bar = new Schema({ body: String }, { id: false });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接