【问题标题】:How to Find the Count one of the Document element in MONGODB [duplicate]如何在 MONGODB 中查找 Document 元素的计数之一 [重复]
【发布时间】:2018-09-30 04:42:20
【问题描述】:

我想获得 Counties.Properties 的确切计数,在我的情况下是 3 ,我尝试了 $size 聚合但没有得到我想要的答案。

{ 
"_id" : ObjectId("234567890"), 
"Version" : "1.1", 
"FileNo" : "1234567", 
"Feature" : [
    "Map", 
    "usr/ammer"
], 
"TVProperties" : [
    "foul"
], 
"Name" : "nice", 
"Countries" : {
    "Properties" : [
        {
            "kyu" : "NAmerica", 
            "triggerString" : "America", 
            "user" : "ME"
        }, 
        {
            "kyu" : "ASIA", 
            "triggerString" : "China", 
            "user" : "You"
        }, 
        {
            "kyu" : "Europe", 
            "triggerString" : "France", 
            "user" : "HE"
        }
    ]
},

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:
    db.collection.aggregate(
    
        // Pipeline
        [
            // Stage 1
            {
                $project: {
                    count: {
                        $size: '$Countries.Properties'
                    },
                    "Version": 1,
                    "FileNo": 1,
                    "Feature": 1,
                    "TVProperties": 1,
                    "Name": 1,
                    "Countries": 1
    
                }
            },
    
        ]
    
    
    );
    

    【讨论】:

    • { "ok" : 0.0, "errmsg" : "$size 的参数必须是一个数组,但类型为:缺失", "code" : NumberInt(17124), "codeName" : "位置 17124" }
    • 试过同样得到上述错误。
    • 这里的Countries是'object'类型
    • @PrashantSharma Country.Properties 是一个数组,我已经计算了它的大小
    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2015-12-17
    • 2021-12-22
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    相关资源
    最近更新 更多