Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用通配符排除MongoDB中的嵌套字段?_Mongodb - Fatal编程技术网

是否可以使用通配符排除MongoDB中的嵌套字段?

是否可以使用通配符排除MongoDB中的嵌套字段?,mongodb,Mongodb,假设我有一份文件: { "_id": "123", "fruit": { "apple": { "species": "Malus pumila", "taste": "Not bad" }, "orange": { "species": "Citrus sinensis", "taste": "Pretty good" } } } 我想按照db.food.find({},{“fr

假设我有一份文件:

{
   "_id": "123",
   "fruit": {
     "apple": {
       "species": "Malus pumila",
       "taste": "Not bad"
     },
     "orange": {
       "species": "Citrus sinensis",
       "taste": "Pretty good"
     }
   }
}
我想按照
db.food.find({},{“fruit.*.taste”:0})
的思路做一些事情,以便在查询结果中不包括味觉字段:

{
   "_id": "123",
   "fruit": {
     "apple": {
       "species": "Malus pumila"
     },
     "orange": {
       "species": "Citrus sinensis"
     }
   }
}

通过传统查询或聚合管道可以实现这种情况吗?

目前无法排除子文档上的通配符。但是,您可以将聚合管道与几个
$project
管道以及
$objectToArray
$arrayToObject
操作符一起使用,以获得相同的结果:

db.test.aggregate([
    { $project: { "fruit" : { $objectToArray: "$fruit" } } },
    { $project: { "fruit.v.taste" : 0} },
    { $project: { "fruit" : { $arrayToObject: "$fruit"} } }
]);
{
        "_id" : "123",
        "fruit" : {
                "apple" : {
                        "species" : "Malus pumila"
                },
                "orange" : {
                        "species" : "Citrus sinensis"
                }
        }
}
第一个$project
{“fruit”:{$objectToArray:“$fruit”}
fruit
文档转换为键/值数组:

{
        "_id" : "123",
        "fruit" : [
                {
                        "k" : "apple",
                        "v" : {
                                "species" : "Malus pumila",
                                "taste" : "Not bad"
                        }
                },
                {
                        "k" : "orange",
                        "v" : {
                                "species" : "Citrus sinensis",
                                "taste" : "Pretty good"
                        }
                }
        ]
}
一旦我们以数组格式得到它,我们就可以用标准投影排除
taste
字段,排除字段
{$project:{“fruit.v.taste”:0}

{
        "_id" : "123",
        "fruit" : [
                {
                        "k" : "apple",
                        "v" : {
                                "species" : "Malus pumila"
                        }
                },
                {
                        "k" : "orange",
                        "v" : {
                                "species" : "Citrus sinensis"
                        }
                }
        ]
}
然后,我们可以使用
$arrayToObject
操作符构建文档

更多信息可在此处找到: $arrayToObject-
$objectToArray-

谢谢,我开始朝这个方向前进。我只需要说服ops团队将所有内容升级到3.6。。。