需要根据mongodb中的值计算字段

时间:2017-01-25 08:29:47

标签: mongodb

我们有mongo 3.2。我们有集合名称test5。 它有许多字段和数组(undaries.couies.ZIPCodes.status)

状态字段的值很少,例如Add1& ADD2

我想根据status = ADD1或ADD2

取COUNT
"**undaries**" : [
    {
        "**couies**" : [
            {
                "**ZIPCodes**" : [
                    {
                        "ZIPCode" : "60349", 
                        "city" : "Test", 
                        "household" : "Test2", 
                        "accounts" : "0", 
                        "SD" : "Y", 
                        "**status**" : "Add1", 
                        "lastUpdateDate" : "2017-01-24T09:39:56.417Z", 
                        "lastUpdateBy" : "Test"
                    }, 
                    {
                        "ZIPCode" : "60234", 
                        "city" : "Test", 
                        "household" : "test1", 
                        "accounts" : "0", 
                        "SD" : "Y", 
                        "status" : "Add2", 
                        "lastUpdateDate" : "2017-01-24T09:39:56.417Z", 
                        "lastUpdateBy" : "Test"
                    },
                    {
                        "ZIPCode" : "60235", 
                        "city" : "Test", 
                        "household" : "test1", 
                        "accounts" : "0", 
                        "SD" : "Y", 
                        "status" : "Add1", 
                        "lastUpdateDate" : "2017-01-24T09:39:56.417Z", 
                        "lastUpdateBy" : "Test"
                    }................

如何根据价值获得状态总数。

谢谢&问候。

1 个答案:

答案 0 :(得分:0)

您可以使用count()方法和$in运算符

db.yourCollection.count({"undaries.couies.ZIPCodes.status":{$in : ["Add1", "Add2"]}})

count().find({query}).count()

的简写