提问者:小点点

省略数组中的某些字段mongo查询


类似于用Mongo在数组中查询对象中的字段?询问者想要查询对象中的特定字段,但我想要的与他相反。

我的数据

{   "_id": "ffb76dea811a6773c9481fd35262fe7f", 
    "fieldA" : "A", 
    "fieldB" : "B", 
    "fieldC" : [ 
        { "group1" : "100", "group2" : "200", "id" : "b0a05edd3cb0d35674174f34da1b3021"}, 
        { "group1" : "110", "group2" : "230", "id" : "b9071b34c21f948e69cb39df7dbf10a7"} 
    ]
}

如何查询获取

{
    "fieldA" : "A", 
    "fieldC" : [ 
        { "group1" : "100", "group2" : "200" }, 
        { "group1" : "110", "group2" : "230" } 
    ]
}

关键点是我不希望id出现在我的查询结果中

db.test.insertOne({"_id":"ffb76dea811a6773c9481fd35262fe7f","fieldA":"A","fieldB":"B","fieldC":[{"group1":"100","group2":"200","id":"b0a05edd3cb0d35674174f34da1b3021"},{"group1":"110","group2":"230","id":"b9071b34c21f948e69cb39df7dbf10a7"}]})

共1个答案

匿名用户

mongoDB中find的第二个参数是投影选项,用于从结果中包含或排除字段。在您的情况下,您需要按如下方式使用它:

db.test.find({},{"fieldC.id":0, fieldB: 0, "_id":0})

请参阅此处了解投影的工作原理https://docs.mongodb.com/manual/reference/method/db.collection.find/