node.js 用猫鼬查询和求和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25557966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:49:50  来源:igfitidea点击:

Query and sum all with mongoose

node.jsmongodbexpressmongoose

提问by Lazy

I want to fetch all users user_totaldocsand user_totalthingsand want to sumthose variables.

我想获取所有用户user_totaldocs,并user_totalthings和要总结这些变量。

How can it's possible? Here is user schema:

怎么可能?这是用户架构:

var user_schema = mongoose.Schema({
    local : {
        ...
        ...
        user_id          : String,
        user_totaldocs   : Number,
        user_totalthings     : Number
        ....

    }
});

回答by Stennie

You can use the Aggregation Pipelineto add calculated fields to a result. There are some examples below using the mongoshell, but the syntax in Mongoose's Aggregate() helperis similar.

您可以使用聚合管道将计算字段添加到结果中。下面有一些使用mongoshell 的示例,但 Mongoose 的Aggregate() 助手中的语法是相似的。

For example, to calculate sums (per user document) you can use the $addexpressionin a $projectstage:

例如,要计算总和(每个用户文档),您可以在阶段中使用$add表达式$project

db.user.aggregate(
    // Limit to relevant documents and potentially take advantage of an index
    { $match: {
        user_id: "foo"
    }},

    { $project: {
        user_id: 1,
        total: { $add: ["$user_totaldocs", "$user_totalthings"] }
    }}
)

To calculate totals across multiple documents you need to use a $groupstagewith a $sumaccumulator, for example:

要计算多个文档的总数,您需要使用带有accumulator$groupstage,例如:$sum

db.user.aggregate(
    { $group: {
        _id: null,
        total:       { $sum: { $add: ["$user_totaldocs", "$user_totalthings"] } },
        totaldocs:   { $sum: "$user_totaldocs" },
        totalthings: { $sum: "$user_totalthings" }
    }}
)

You may want only the one totalfield; I've added in totaldocsand totalthingsas examples of calculating multiple fields.

您可能只需要一个total字段;我已添加totaldocstotalthings作为计算多个字段的示例。

A group _idof nullwill combine values from all documents passed to the $groupstage, but you can also use other criteria here (such as grouping by user_id).

A组_idnull将传递给所有文档合并值$group阶段,但你也可以在这里使用其他标准(如通过分组user_id)。

回答by Danish

You can use aggregation framework provided by mongodb. For your case --

您可以使用 mongodb 提供的聚合框架。对于你的情况——

if you want to fetch sum of user_totaldocs and sum of user_totalthings across the collection (meaning for all users), do --

如果您想获取整个集合中 user_totaldocs 的总和和 user_totalthings 的总和(意味着所有用户),请执行--

db.user_schemas.aggregate(
 [
  {
    $group : {
       user_id : null,
       user_totaldocs: { $sum: "$user_totaldocs"}, // for your case use local.user_totaldocs
       user_totalthings: { $sum: "$user_totalthings" }, // for your case use local.user_totalthings
       count: { $sum: 1 } // for no. of documents count
    }
  }
])

To sum user_totaldocs and user_totalthings for particular user in a collection(assuming there are multiple document for a user), this will return sum for each user, DO --

对集合中特定用户的 user_totaldocs 和 user_totalthings 求和(假设一个用户有多个文档),这将返回每个用户的总和,做——

db.user_schemas.aggregate(
 [
  {
    $group : {
       user_id : "$user_id", 
       user_totaldocs: { $sum: "$user_totaldocs"}, // for your case use local.user_totaldocs
       user_totalthings: { $sum: "$user_totalthings" }, // for your case use local.user_totalthings
       count: { $sum: 1 } // for no. of documents count
    }
  }
])

No need to provide individual user id.

无需提供个人用户 ID。

For more info read: 1. http://docs.mongodb.org/manual/reference/operator/aggregation/group/#pipe._S_group2. http://docs.mongodb.org/manual/core/aggregation/

有关更多信息,请阅读:1. http://docs.mongodb.org/manual/reference/operator/aggregation/group/#pipe._S_group2. http://docs.mongodb.org/manual/core/aggregation/