【问题标题】:How to find count of a key in one document using mongoDB?如何使用 mongoDB 在一个文档中查找键的计数?
【发布时间】:2015-01-29 05:15:47
【问题描述】:

我的收藏中有以下结构:

users:[
  {
    "name":"ABC",
    "address":{
    "city":"London",
    "country":"UK",
    }
  },
  {
    "name":"XYZ",
    "address":{
      "city":"London",
      "country":"UK",
     }
  },
  {
    "name":"PQR",
    "address":{
    "city":"NewYork",
    "country":"US",
    }
  }
]

我想要计算 'address' 和 'name' 中的 'city' 键的出现次数。

我想查询上面的集合并想要以下输出:

[{
"name":"ABC",
"city":"London",
"count":2
},{
"name":"XYZ",
"city":"London",
"count":2
}, {
"name":"PQR",
"city":"NewYork",
"count":1
}
]

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    我模拟了你的收藏

    {
        "_id" : ObjectId("547c30ae371ea419f07b9550"),
        "users" : [ 
            {
                "name" : "ABC",
                "address" : {
                    "city" : "London",
                    "country" : "UK"
                }
            }, 
            {
                "name" : "XYZ",
                "address" : {
                    "city" : "London",
                    "country" : "UK"
                }
            }, 
            {
                "name" : "PQR",
                "address" : {
                    "city" : "NewYork",
                    "country" : "US"
                }
            }
        ]
    }
    

    然后我使用aggregate framework

    db.coll.aggregate([
    
    {
      $unwind:"$users"  
    },
    {
        $group:{
            _id:"$users.address.city",
            name:{$push:"$users.name"},
            city:{$first:"$users.address.city"},
            count:{$sum:1}
        }
    },{
        $unwind:"$name"
    },{
        $project:{
            _id:0,
            "city":"$_id",
            "name":1,
            "city":1,
            "count":1
        }
    
    }])
    

    结果:

    {
        "result" : [ 
            {
                "name" : "PQR",
                "city" : "NewYork",
                "count" : 1
            }, 
            {
                "name" : "ABC",
                "city" : "London",
                "count" : 2
            }, 
            {
                "name" : "XYZ",
                "city" : "London",
                "count" : 2
            }
        ],
        "ok" : 1
    }
    

    问题后更新

    我添加了一个新文档

    {
        "_id" : ObjectId("547c394c371ea419f07b9551"),
        "users" : [ 
            {
                "address" : {
                    "city" : "Livorno",
                    "country" : "LI"
                }
            }, 
            {
                "address" : {
                    "city" : "Livorno",
                    "country" : "LI"
                }
            }, 
            {
                "address" : {
                    "city" : "NewYork",
                    "country" : "US"
                }
            }
        ]
    }
    

    和新的查询

    db.coll.aggregate([
    
    {
      $unwind:"$users"  
    },
    {
        $group:{
            _id:"$users.address.city",        
            "name": {
                    $push:{"$ifNull": ["$users.name","$_id"]}
               },
            city:{$first:"$users.address.city"},
            count:{$sum:1}
        }
    },{
        $unwind:"$name"
    },{
        $project:{
            _id:0,
            "city":"$_id",
            "name":1,
            "city":1,
            "count":1
        }
    
    }])
    

    结果:

    {
        "result" : [ 
            {
                "name" : "PQR",
                "city" : "NewYork",
                "count" : 2
            }, 
            {
                "name" : ObjectId("547c394c371ea419f07b9551"),
                "city" : "NewYork",
                "count" : 2
            }, 
            {
                "name" : ObjectId("547c394c371ea419f07b9551"),
                "city" : "Livorno",
                "count" : 2
            }, 
            {
                "name" : ObjectId("547c394c371ea419f07b9551"),
                "city" : "Livorno",
                "count" : 2
            }, 
            {
                "name" : "ABC",
                "city" : "London",
                "count" : 2
            }, 
            {
                "name" : "XYZ",
                "city" : "London",
                "count" : 2
            }
        ],
        "ok" : 1
    }
    

    【讨论】:

    • 如果'users'下的任何对象中都不存在键名,它会起作用吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2019-10-30
    • 2023-02-09
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多