Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Database Mongodb:如何;“展平”;一些查询结果_Database_Mongodb_Aggregation Framework - Fatal编程技术网

Database Mongodb:如何;“展平”;一些查询结果

Database Mongodb:如何;“展平”;一些查询结果,database,mongodb,aggregation-framework,Database,Mongodb,Aggregation Framework,帮助在查询中“展平”(拉取与文档字段处于同一级别的嵌套字段)mongodb文档 //this is "anagrafiche" collection [{ "name": "tizio" ,"surname": "semproni" ,"birthday": "01/02/1923" ,"home": { "road": "via" ,"roadname": "bianca" ,"roadN": 12 ,"city": "rome" ,

帮助在查询中“展平”(拉取与文档字段处于同一级别的嵌套字段)mongodb文档

//this is "anagrafiche" collection
[{
 "name": "tizio"
,"surname": "semproni"
,"birthday": "01/02/1923"
,"home": {
     "road": "via"
    ,"roadname": "bianca"
    ,"roadN": 12
        ,"city": "rome"
        ,"country": "italy"
        }
},
{
 "name": "caio"
,"surname": "giulio"
,"birthday": "02/03/1932"
,"home": {
     "road": "via"
    ,"roadname": "rossa"
    ,"roadN": 21
        ,"city": "milan"
        ,"country": "italy"
        }
},
{
 "name": "mario"
 ,"surname": "rossi"
// birthday is not present for this document
,"home": {
     "road": "via"
    ,"roadname": "della pace"
    ,"roadN": 120
        ,"city": "rome"
        ,"country": "italy"
        }
}
]
我的问题是:

db.anagrafiche.aggregate([  {$match {"home.city": "rome"}}
                {$project:{"name": 1, "surname":1, <an expression to flatten the address>, "birthday": 1, "_id":0}}
             ]
);
您可以使用获取嵌套文档键和值,然后与一起使用动态连接值:

db.collection.aggregate([
    {
        $project: {
            _id: 0,
            name: 1,
            surname: 1,
            birthday: 1,
            address: {
                $reduce: {
                    input: { $objectToArray: "$home" },
                    initialValue: "",
                    in: {
                        $concat: [
                            "$$value",
                            { $cond: [ { $eq: [ "$$value", "" ] }, "", " " ] },
                            { $toString: "$$this.v" }
                        ]
                    }
                }
            }
        }
    }
])

$arrayToObject或$objectToArray?还有:为什么要用双美元符号
$$
?当访问
$$value
$$this.v
时,没有一个
$
?它也在文档中使用,但没有解释原因。单美元表示字段,双美元符号表示变量,
$reduce
引入两个变量
$$value
(状态)和
$$this
(当前处理的元素)
db.collection.aggregate([
    {
        $project: {
            _id: 0,
            name: 1,
            surname: 1,
            birthday: 1,
            address: {
                $reduce: {
                    input: { $objectToArray: "$home" },
                    initialValue: "",
                    in: {
                        $concat: [
                            "$$value",
                            { $cond: [ { $eq: [ "$$value", "" ] }, "", " " ] },
                            { $toString: "$$this.v" }
                        ]
                    }
                }
            }
        }
    }
])