Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组中的mongoDB字符串转换为双精度_Mongodb_Aggregation Framework - Fatal编程技术网

数组中的mongoDB字符串转换为双精度

数组中的mongoDB字符串转换为双精度,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有一个mongoDB,其中包含如下条目: { "_id" : ObjectId("5cf3e2e0839abf5afbf05052"), "sex" : "Male", "location" : { "city" : "Solingen", "address" : { "streetname" : "Dawn", "streetnumb

我有一个mongoDB,其中包含如下条目:

{
        "_id" : ObjectId("5cf3e2e0839abf5afbf05052"),
        "sex" : "Male",
        "location" : {
            "city" : "Solingen",
            "address" : {
                "streetname" : "Dawn",
                "streetnumber" : "888"
            }
        },
        "credit" : [
            {
                "type" : "switch",
                "number" : "201864776633337",
                "currency" : "CNY",
                "balance" : "4898.89"
            },
            {
                "type" : "jcb",
                "number" : "3552382704063930",
                "currency" : "IDR",
                "balance" : "4501.62"
            }
        ]
    },
    {
        "_id" : ObjectId("5cf3e2e0839abf5afbf051c6"),
        "sex" : "Male",
        "location" : {
            "city" : "Hamburg",
            "address" : {
                "streetname" : "Brentwood",
                "streetnumber" : "6"
            }
        },
        "nationality" : "Germany",
        "credit" : [
            {
                "type" : "jcb",
                "number" : "4017959913393",
                "currency" : "SEK",
                "balance" : "3867.38"
            },
            {
                "type" : "jcb",
                "number" : "5100136044479699",
                "currency" : "CNY",
                "balance" : "4323.61"
            }
        ]
    },
我想使用卡的总平均值和总金额来执行

因此,我的代码应该映射卡片数组,并在卡片上增加平衡

我尝试使用映射和合并对象

    [
        { "$addFields": {
            "credit": {
              "$map": {
                "input": "$credit",
                "in": {
                  "$mergeObjects": [
                    "$$this",
                    {
                      "convertedBalance": {
                        "$toDouble": "$$this.balance"
                      }
                    }
                  ]
                }
              }
            }
          }},
        { $unwind : "$credit" },
        { $match : { sex : "Female", nationality : "Poland"}},
        {$group : { _id : "$credit.currency", avgBalance : {$avg : "$convertedBalance"}, sumBalance : {$sum : "$convertedBalance"}}}
    ]
).toArray());
但是
avgBalance
的结果是
null
,而
sumBalance
的结果是0,如下所示:

connecting to: mongodb://localhost:27017/nbd?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b49b33ce-808b-4814-a31e-7a70ce6fe0d7") }
MongoDB server version: 4.0.10
[
    {
        "_id" : "MYR",
        "avgBalance" : null,
        "sumBalance" : 0
    },
    {
        "_id" : "ALL",
        "avgBalance" : null,
        "sumBalance" : 0
    },
输出应该是这样的:

        "_id" : "AFN",
        "avgBalance" : 5585.163333333333,
        "sumBalance" : 16755.489999999998
    },

当您在引用不存在的根级别字段时运行
$group
阶段
convertedBalance
嵌套在
credit
中时,请尝试:

{
    $group : { 
        _id : "$credit.currency", 
        avgBalance : {$avg : "$credit.convertedBalance"}, 
        sumBalance : {$sum : "$credit.convertedBalance"}
    }
}