node.js - Mongoose:如何在填充期间排除虚拟 id 字段

标签 node.js mongodb mongoose

我试图排除 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/

相关文章:

javascript - mongodb按范围查询分页与另一个排序字段

javascript - 如何使用 javascript(客户端)中的变量从 node.js(服务器端)检索数据?

node.js - 在 Angular 应用程序中,我不断收到此错误 Cannot find module 'fs'

node.js - 范围错误[ERR_FS_FILE_TOO_LARGE] : File size (3472064213) is greater than 2 GB

node.js - NodeJs 和 ExpressJs 无法分离错误和响应处理中使用的公共(public)代码

java - 获取 JSON 作为输入时出现 MongoDB NULL 指针异常

java - 如何更改 .JSON 文件中的某些值,然后在保留 JSON 格式的情况下将其写回? ( java )

java - 使用 Java 驱动程序更新 MongoDB 中子字段的值?

node.js - 使用导入而不是 require() 时带有 uuid 的 MongooseError

javascript - 我的文档何时使用 Mongoose 保存功能保存?