Node.js 获取MongoDB中的值之和

Node.js 获取MongoDB中的值之和,node.js,mongodb,aggregation-framework,Node.js,Mongodb,Aggregation Framework,我有一个数组,数组中的每个列表项都是一个对象,它有一个值键。我想做的是把所有的值相加得到一个总数。我想在后端而不是前端执行此操作。我尝试过聚合方法,但没有任何运气,因为它返回一个空数组。这是我的阵列: "budgets" : [ { "name" : "check", "value" : "1000", "expense" : "false", "uniqueId" : 0.9880268634296954 },

我有一个数组,数组中的每个列表项都是一个对象,它有一个值键。我想做的是把所有的值相加得到一个总数。我想在后端而不是前端执行此操作。我尝试过聚合方法,但没有任何运气,因为它返回一个空数组。这是我的阵列:

"budgets" : [
    {
        "name" : "check",
        "value" : "1000",
        "expense" : "false",
        "uniqueId" : 0.9880268634296954
    },
    {
        "name" : "car",
        "value" : "500",
        "expense" : "true",
        "uniqueId" : 0.1904486275743693
    },
    {
        "name" : "car2",
        "value" : "500",
        "expense" : "false",
        "uniqueId" : 0.23043920518830419
    },
    {
        "name" : "car23",
        "value" : "500",
        "expense" : "false",
        "uniqueId" : 0.014449386158958077
    },
    {
        "name" : "car235",
        "value" : "500",
        "expense" : "false",
        "uniqueId" : 0.831609656335786
    }
],

我想做的是得到包含“费用”的值的总和:“真”和单独的“费用”总和:“假”我该怎么做?就像我说的,我已经尝试过聚合方法,但我一定是做错了。

试试这样的方法

db.collection.aggregate([ { "$unwind": "$budgets" },
                  { $match: { expense: "true" } },
                  { $group: { _id: "$_id",
                            name: { $addToSet: "$name" },
                            uniqueId: { $addToSet: "$uniqueId" }, 
                            total: { $sum: "$$budgets.value" } } }
               ])
{
    "result" : [
        {
            "_id" : "true",
            "total" : 500
        },
        {
            "_id" : "false",
            "total" : 2000
        }
    ],
    "ok" : 1
}

试试这样的

db.collection.aggregate([ { "$unwind": "$budgets" },
                  { $match: { expense: "true" } },
                  { $group: { _id: "$_id",
                            name: { $addToSet: "$name" },
                            uniqueId: { $addToSet: "$uniqueId" }, 
                            total: { $sum: "$$budgets.value" } } }
               ])
{
    "result" : [
        {
            "_id" : "true",
            "total" : 500
        },
        {
            "_id" : "false",
            "total" : 2000
        }
    ],
    "ok" : 1
}

试试这样的

db.collection.aggregate([ { "$unwind": "$budgets" },
                  { $match: { expense: "true" } },
                  { $group: { _id: "$_id",
                            name: { $addToSet: "$name" },
                            uniqueId: { $addToSet: "$uniqueId" }, 
                            total: { $sum: "$$budgets.value" } } }
               ])
{
    "result" : [
        {
            "_id" : "true",
            "total" : 500
        },
        {
            "_id" : "false",
            "total" : 2000
        }
    ],
    "ok" : 1
}

试试这样的

db.collection.aggregate([ { "$unwind": "$budgets" },
                  { $match: { expense: "true" } },
                  { $group: { _id: "$_id",
                            name: { $addToSet: "$name" },
                            uniqueId: { $addToSet: "$uniqueId" }, 
                            total: { $sum: "$$budgets.value" } } }
               ])
{
    "result" : [
        {
            "_id" : "true",
            "total" : 500
        },
        {
            "_id" : "false",
            "total" : 2000
        }
    ],
    "ok" : 1
}

如果您不熟悉使用聚合框架命令,您可能还不知道该操作符。您可以使用此选项来分离总计:

db.collection.aggregate([
//展开数组以取消项目规格化
{“$unwind”:“$budgets”},
//将总数与条件分组
{“$组”:{
“\u id”:“$\u id”,
“费用”:{“$sum”:{
“$cond”:[
“$budgets.expense”,
“$budgets.value”,
0
]
}},
“非费用”:{“$sum”:{
“$cond”:[
“$budgets.expense”,
0,
“$budgets.value”
]
}}
}}
])
因此,这将要做的是计算费用的
真/假
条件作为第一个参数,当条件实际为
时,将选择的第二个参数并传递给
$sum
。如果条件的计算结果为
false
,则选择第二个参数

但从您的数据来看,您似乎有一个问题:

{
“姓名”:“支票”,
“值”:“1000”,
“费用”:“假”,
“唯一标识”:0.9880268634296954
},
请注意,所有字段,尤其是
费用
价值
项,都是字符串。因此,这是一个问题,因为虽然我们可以通过字符串比较而不是直接布尔运算来计算
true/false
值,但您无法将字符串作为可以传递给的数字


因此,首先您需要修复数据,除非它实际上不是您所表示的形式。但是当它符合正确的表单时,您可以进行聚合。

如果您不熟悉使用聚合框架命令,您可能还不知道该操作符。您可以使用此选项来分离总计:

db.collection.aggregate([
//展开数组以取消项目规格化
{“$unwind”:“$budgets”},
//将总数与条件分组
{“$组”:{
“\u id”:“$\u id”,
“费用”:{“$sum”:{
“$cond”:[
“$budgets.expense”,
“$budgets.value”,
0
]
}},
“非费用”:{“$sum”:{
“$cond”:[
“$budgets.expense”,
0,
“$budgets.value”
]
}}
}}
])
因此,这将要做的是计算费用的
真/假
条件作为第一个参数,当条件实际为
时,将选择的第二个参数并传递给
$sum
。如果条件的计算结果为
false
,则选择第二个参数

但从您的数据来看,您似乎有一个问题:

{
“姓名”:“支票”,
“值”:“1000”,
“费用”:“假”,
“唯一标识”:0.9880268634296954
},
请注意,所有字段,尤其是
费用
价值
项,都是字符串。因此,这是一个问题,因为虽然我们可以通过字符串比较而不是直接布尔运算来计算
true/false
值,但您无法将字符串作为可以传递给的数字


因此,首先您需要修复数据,除非它实际上不是您所表示的形式。但是当它符合正确的表单时,您可以进行聚合。

如果您不熟悉使用聚合框架命令,您可能还不知道该操作符。您可以使用此选项来分离总计:

db.collection.aggregate([
//展开数组以取消项目规格化
{“$unwind”:“$budgets”},
//将总数与条件分组
{“$组”:{
“\u id”:“$\u id”,
“费用”:{“$sum”:{
“$cond”:[
“$budgets.expense”,
“$budgets.value”,
0
]
}},
“非费用”:{“$sum”:{
“$cond”:[
“$budgets.expense”,
0,
“$budgets.value”
]
}}
}}
])
因此,这将要做的是计算费用的
真/假
条件作为第一个参数,当条件实际为
时,将选择的第二个参数并传递给
$sum
。如果条件的计算结果为
false
,则选择第二个参数

但从您的数据来看,您似乎有一个问题:

{
“姓名”:“支票”,
“值”:“1000”,
“费用”:“假”,
“唯一标识”:0.9880268634296954
},
请注意,所有字段,尤其是
费用
价值
项,都是字符串。因此,这是一个问题,因为我们可以通过字符串比较而不是直接布尔运算来计算
true/false
值,而只需c