mongo聚合与嵌套字段

时间:2017-07-20 18:41:28

标签: arrays mongodb mongodb-query

我的字段看起来像这样

{
"_id" : 12345,
"_class" : "com.Location",
"locations" : [
    {
        "longitude" : -12345675433,
        "latitude" : 12345675433,
        "ts" : NumberLong("134546456"),
        "fullAddress" : "123456 road United States",
        "Phone" : "123456677",
        "Name" : "Upagna",
        "accuracy" : 0
    },
    {
        "longitude" : -12345675433,
        "latitude" : 12345675433,
        "ts" : NumberLong("134546456"),
        "fullAddress" : "123456 road United States",
        "Phone" : "123456677",
        "Name" : "Upagna",
        "accuracy" : 0
    },

最终结果将是每个ID的ID和位置数以及每个ID的电话。我能够计算出位置的数量,但无法将手机添加到最终结果

db.locations.aggregate([{$project : {_id: 0, id: "$_id", total: {$size: "$locations", }}}])

结束reult应该看起来像这样

{ "id" : 36761, "total" : 2 ,"Phone" : "123456677" }
{ "id" : 36904, "total" : 8 ,"Phone" : "123456677"}
{ "id" : 36945, "total" : 7 ,"Phone" : "123456677"}
{ "id" : 37095, "total" : 1 ,"Phone" : "123456677"}

任何帮助都会让我欣赏..

谢谢

0 个答案:

没有答案