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

时间:2017-11-29 03:13:45

标签: mongodb pymongo

类似于Query for a field in an object in array with 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 个答案:

答案 0 :(得分:0)

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

projection

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