【问题标题】:Exclude mongoose table filed in nested object排除嵌套对象中的猫鼬表字段
【发布时间】:2016-03-24 08:05:41
【问题描述】:

当我有简单的架构时,我使用这种方法来获取所有没有_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(...));

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    我找到了解决方案。基于从此处排除字段的替代语法: https://stackoverflow.com/a/24389009/3306465

    这对我有用:

    anotherSchema.find({}, '-_id -testField._id', function(...))
    

    【讨论】:

    • 由于某种原因这对我不起作用:/。它只删除第一个参数,但不删除第二个参数
    【解决方案2】:

    我认为这应该可行:

    anotherSchema.find({}, {testField._id: 0}, function(...));
    

    【讨论】:

    • 这给了我SyntaxError: Unexpected token .的错误
    猜你喜欢
    • 1970-01-01
    • 2015-09-15
    • 2018-10-15
    • 2021-08-23
    • 2013-10-07
    • 2017-01-28
    • 1970-01-01
    • 2014-02-10
    • 2014-07-13
    相关资源
    最近更新 更多