当我有简单的架构时,我使用此方法来获取没有 _id
字段的所有字段:
var testSchema = mongoose.Schema({
field1: String
});
testSchema.find({}, { _id: 0, _v: 0}, function(...));
但是当我有一个嵌套对象时,当我查询它时,它的 _id
字段就会出现。
我该如何做同样的事情 - 隐藏嵌套对象的 _id
归档?
var anotherSchema = mongoose.Schema({
field2: String,
testField: [testSchema]
});
anotherSchema.find({}, { ???? }, function(...));
最佳答案
我已经找到解决办法了。基于从此处排除字段的替代语法: https://stackoverflow.com/a/24389009/3306465
这对我有用:
anotherSchema.find({}, '-_id -testField._id', function(...))
关于node.js - 排除嵌套对象中的 Mongoose 表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34341634/