Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/14.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 sum()数据_Mongodb_Aggregation Framework_Nosql - Fatal编程技术网

MongoDB sum()数据

MongoDB sum()数据,mongodb,aggregation-framework,nosql,Mongodb,Aggregation Framework,Nosql,我不熟悉mongoDB和nosql,求和的语法是什么 在MySQL中,我会这样做: SELECT SUM(amount) from my_table WHERE member_id = 61; 我如何将其转换为MongoDB?以下是我尝试过的: db.bigdata.aggregate({ $group: { _id: { memberId: 61, total: {$sum: "$amount"} }

我不熟悉mongoDB和nosql,求和的语法是什么

在MySQL中,我会这样做:

SELECT SUM(amount) from my_table WHERE member_id = 61;
我如何将其转换为MongoDB?以下是我尝试过的:

db.bigdata.aggregate({
    $group: {
        _id: {
            memberId: 61, 
            total: {$sum: "$amount"}
        }
    }
})
用于您想要的参考:

db.bigdata.aggregate(
{
    $match: {
        memberId: 61
    }
},
{
    $group: {
        _id: "$memberId",
        total : { $sum : "$amount" }
    }
})
从MongoDB文档:

聚合管道是基于数据处理管道概念建模的数据聚合框架。文档进入一个多阶段管道,该管道将文档转换为聚合结果

用于您想要的参考:

db.bigdata.aggregate(
{
    $match: {
        memberId: 61
    }
},
{
    $group: {
        _id: "$memberId",
        total : { $sum : "$amount" }
    }
})
从MongoDB文档:

聚合管道是基于数据处理管道概念建模的数据聚合框架。文档进入一个多阶段管道,该管道将文档转换为聚合结果


最好先进行匹配,然后再进行分组,这样系统就只能对过滤后的记录执行分组操作。如果您首先执行组操作,则系统将对所有记录执行组操作,然后选择memberId=61的记录

db.bigdata.aggregate( 
{ $match : {memberId : 61 } },
{ $group : { _id: "$memberId" , total : { $sum : "$amount" } } }                       
)

最好先进行匹配,然后再进行分组,这样系统就只能对过滤后的记录执行分组操作。如果您首先执行组操作,则系统将对所有记录执行组操作,然后选择memberId=61的记录

db.bigdata.aggregate( 
{ $match : {memberId : 61 } },
{ $group : { _id: "$memberId" , total : { $sum : "$amount" } } }                       
)
如果要对不属于数组的数据求和,如果要对文档中某个数组中的数据求和,则使用

db.collectionName.aggregate(
{$unwind:"$arrayName"},   //unwinds the array element

{
$group:{_id: "$arrayName.arrayField", //id which you want to see in the result
total: { $sum: "$arrayName.value"}}   //the field of array over which you want to sum
})
并且会得到这样的结果

{
    "result" : [
        {
            "_id" : "someFieldvalue",
            "total" : someValue
        },
        {
            "_id" : "someOtherFieldvalue",
            "total" : someValue
        }
    ],
    "ok" : 1
}
如果要对不属于数组的数据求和,如果要对文档中某个数组中的数据求和,则使用

db.collectionName.aggregate(
{$unwind:"$arrayName"},   //unwinds the array element

{
$group:{_id: "$arrayName.arrayField", //id which you want to see in the result
total: { $sum: "$arrayName.value"}}   //the field of array over which you want to sum
})
并且会得到这样的结果

{
    "result" : [
        {
            "_id" : "someFieldvalue",
            "total" : someValue
        },
        {
            "_id" : "someOtherFieldvalue",
            "total" : someValue
        }
    ],
    "ok" : 1
}

令人惊叹的谢谢注意:
\u id:“$memberId,”
逗号位置错误。聚合函数的参数可以作为数组或单独的参数(2.6)好的,我之前查看的页面让我困惑了一会儿,但您的示例太棒了!令人惊叹的谢谢注意:
\u id:“$memberId,”
逗号位置错误。聚合函数的参数可以作为数组或单独的参数(2.6)好的,我之前查看的页面让我困惑了一会儿,但您的示例太棒了!