Mongo forEach查询

时间:2018-02-20 13:19:07

标签: json database mongodb aggregation-framework

我有下面你可以看到的JSON,我想要对两个对象的值求和,但是当我进行聚合时,它会返回0.这里你可以看到我使用的查询;真的第一行我只用它来确保路径有效,而且确实如此。另一方面,当我在聚合查询中使用此路径时,它会为我提供" ID"和" COUNT"正确的价值观,但" SUM"当它必须是3600时总是0。任何想法?

db.getCollection('TEST').find({"prices.year.months.day.csv.price.valPrice":1800})

db.TEST.aggregate([
{ $match: {"location.cp":"20830"}},
    {$group:{_id:"20830",total:{$sum:"$prices.year.months.day.csv.price.valPrice"}, count: { $sum: 1 }

}}])

这是JSON:

   {
        "_id" : "20830:cas:S:3639",
        "lodgtype" : "Casa",
        "lodg" : "Motrico: country holiday home - San sebastian",
        "webid" : "6107939",
        "location" : {
            "thcod" : "20",
            "cp" : "20830",
            "th" : "Gipuzkoa",
            "geometry" : {
                "type" : "Point",
                "coordinates" : [ 
                    43.31706238, 
                    -2.40293598
                ]
            }
        },
        "prices" : {
            "year" : [ 
                {
                    "valYear" : "2018",
                    "months" : [ 
                        {
                            "valMonth" : "02",
                            "day" : [ 
                                {
                                    "valDay" : "13",
                                    "csv" : [ 
                                        {
                                            "valCsv" : "20180205210908_223",
                                            "price" : [ 
                                                {
                                                    "valPrice" : 1800.0
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "reg" : {
            "created" : "20180213",
            "updated" : "20180213",
            "viewed" : "20180213"
        }
    },{
    "_id" : "TEST20830:cas:S:3639",
    "lodgtype" : "Casa",
    "lodg" : "TESTMotrico: country holiday home - San sebastian",
    "webid" : "6107930",
    "location" : {
        "thcod" : "20",
        "cp" : "20830",
        "th" : "Gipuzkoa",
        "geometry" : {
            "type" : "Point",
            "coordinates" : [ 
                43.31706238, 
                -2.40293598
            ]
        }
    },
    "prices" : {
        "year" : [ 
            {
                "valYear" : "2018",
                "months" : [ 
                    {
                        "valMonth" : "02",
                        "day" : [ 
                            {
                                "valDay" : "13",
                                "csv" : [ 
                                    {
                                        "valCsv" : "20180205210908_223",
                                        "price" : [ 
                                            {
                                                "valPrice" : 1800.0
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    },
    "reg" : {
        "created" : "20180213",
        "updated" : "20180213",
        "viewed" : "20180213"
    }
}

1 个答案:

答案 0 :(得分:3)

由于您已经深度嵌套了数组,因此您需要展开以展平为文档结构。要计算您$match$push$$ROOT {{}}}之后使用额外群组的匹配数量,以保留匹配数据。

db.TEST.aggregate([
  {"$match":{"location.cp":"20830"}},
  {"$group":{
    "_id":"20830",
    "data":{"$push":"$$ROOT"},
    "count":{"$sum":1}
  }},
  {"$unwind":"$data.prices.year"},
  {"$unwind":"$data.prices.year"},
  {"$unwind":"$data.prices.year.months"},
  {"$unwind":"$data.prices.year.months.day"},
  {"$unwind":"$data.prices.year.months.day.csv"},
  {"$unwind":"$data.prices.year.months.day.csv.price"},
  {"$group":{
    "_id":"20830",
    "total":{"$sum":"$prices.year.months.day.csv.price.valPrice"},
    "count":{"$first":"$count"}
  }}
])