MongoDB sum()数据 [英] MongoDB sum() data

查看:672
本文介绍了MongoDB sum()数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongoDB和nosql的新手,获取总和的语法是什么?

I am new to mongoDB and nosql, what is the syntax to get a sum?

在MySQL中,我将执行以下操作:

In MySQL, I would do something like this:

SELECT SUM(amount) from my_table WHERE member_id = 61;

我如何将其转换为MongoDB?这是我尝试过的:

How would I convert that to MongoDB? Here is what I have tried:

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

推荐答案

使用 http://docs.mongodb.org/manual/tutorial/aggregation-zip-code-data-set/以供您参考:

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

来自MongoDB文档:

From the MongoDB docs:

聚合管道是一个基于数据处理管道概念建模的数据聚合框架.文档进入多阶段流水线,将文档转换成汇总结果.

The aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into an aggregated results.

这篇关于MongoDB sum()数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆