简体   繁体   English

MongoDB Mongoose 聚合/数学运算

[英]MongoDB Mongoose aggregation / math operations

I am having a problem with understanding how the $aggregation method works in Mongoose.我在理解 Mongoose 中的$aggregation方法如何工作时遇到问题。 To be honest, I couldn't found any code examples in mongoose docs (I haven't found even [search] option on their site [google site:mongoosejs.com helps me])老实说,我在 mongoose 文档中找不到任何代码示例(我什至没有在他们的网站上找到 [search] 选项 [google site:mongoosejs.com 帮助我])

So, I hope someone will help me to explain this and answer my question.所以,我希望有人能帮助我解释这一点并回答我的问题。

For example, I have various documents in my collection, with fields:例如,我的集合中有各种文档,其中包含以下字段:

    { "_id": 1, "item":47139, "total_price": 560000, "quantity": 56, "lastModified" : 1491748073000 }
    { "_id": 3, "item":47140, "total_price": 1750000, "quantity": 150, "lastModified" : 1491748073000 }

and I would like to $sum all the "quantity" for documents with id: 47139 and timestamp: 1491748073000 .我想对id: 47139timestamp: 1491748073000文档的所有“数量”进行$sum As for now this code works fine and provides me the necessary data:至于现在这段代码工作正常,并为我提供了必要的数据:

    var server = mongoose.model('Name_of_my_Schema', Schema);

    server.find({ lastModified : 1491748073000, item : 47139 }, null, {sort: 'buyout'},function (err, res) {
        total = 0;
        for (i = 0; i < res.length; i++) {  //sorry, map.reduce, but not this time
            total += res[i].quantity; 
        }
        console.log(total); //in this case total = 256
    });

but is it possible to do this operation via mongoose?但是可以通过猫鼬来做这个操作吗? According to mongo docs I should use this code for matching the necessary array of docs, like these:根据 mongo docs,我应该使用此代码来匹配必要的文档数组,如下所示:

  server.aggregate().match({
   lastModified : 1491748073000,
    item : 47139
  }).exec(function (err, result){
    console.log(result);
     console.log(err);
  });

and then use the $group to group the necessary data and { $sum: "quantity" } , but what I should do next?然后使用$group将必要的数据和{ $sum: "quantity" }分组,但是接下来我应该做什么? Why should I use a $group, if I just want to receive a sum of all quantity?如果我只想收到所有数量的总和,为什么要使用 $group?

Could someone give me a clue what am I missing?有人可以告诉我我错过了什么吗?

As you have rightly said, you are missing the $group pipeline step to do the sum aggregate.正如您所说的那样,您缺少$group管道步骤来进行总和聚合。 Complete the pipeline as:完成管道如下:

server.aggregate()
    .match({
        "lastModified": 1491748073000,
        "item": 47139
    })
    .group({
        "_id": null,
        "total": { "$sum": "$quantity" }
    })
    .exec(function (err, result){
        console.log(result);
        console.log(err);
    });

or as an array of operators:或作为运算符数组:

server.aggregate([
    { "$match": { "lastModified": 1491748073000, "item": 47139 } },
    { "$group": { "_id": null, "total": { "$sum": "$quantity" } } } 
]).exec(function (err, result){
    console.log(result);
    console.log(err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM