我试图排除 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”字段?因为我在填充中排除了 _id,它还是 null 吗?
最佳答案
您可以在架构选项 (see documentation here) 中禁用虚拟 id
字段。在您的情况下,它看起来像这样:
var Bar = new Schema({ body: String }, { id: false });
关于node.js - Mongoose:如何在填充期间排除虚拟 id 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32939499/