简体   繁体   English

mongodb聚合获取匹配文档的总数

[英]mongodb aggregation get the total number of matched document

I have a following sample docs saved in mongogb, like: 我有以下示例文档保存在mongogb中,例如:

{
    name: 'andy',
    age: 19,
    description: 'aaa aaa aaa'
}
{
    name: 'andy',
    age: 17,
    description: 'bbb bbb bbb'
}
{
    name: 'leo',
    age: 10,
    description: 'aaa aaa aaa'
}
{
    name: 'andy',
    age: 17,
    description: 'ccc ccc ccc'
}

what the pipeline should look like to get the total number of name in each of matched sets? 要获得每个匹配集中的名称总数,管道应该是什么样子? so I can use this sum number for next pipe. 因此我可以将此总和编号用于下一个管道。 the pipeline I currently have is this: 我目前拥有的管道是这样的:

var pip = [
    {
        $match: { name: 'andy' }
    }
]

and I want to get this result like 我想得到这样的结果

{
    name: 'andy',
    age: 19,
    description: 'aaa aaa aaa',
    total_andy: 3
}
{
    name: 'andy',
    age: 17,
    description: 'bbb bbb bbb',
    total_andy: 3
}
{
    name: 'andy',
    age: 17,
    description: 'ccc ccc ccc',
    total_andy: 3
}

I am not exactly clear as to what you want. 我不清楚你想要什么。 And i don't have enough reputation to ask for that in a comment. 而且我没有足够的声誉在评论中要求这样做。 So let me have a shot at answering. 因此,让我尝试一下。 If the answer isn't what you want, clarify the question further and we'll get to it... 如果答案不是您想要的,请进一步说明问题,我们将继续进行...

var term1group = {$group : 
                            {'_id' : '$name'},
                            'total_names' : {$sum : 1},
                            'ageAndDescription' : {$addToSet : {'$age', '$description'}}
                    }

var term2unwind = {$unwind : '$ageAndDescription'}

var term3project = {$project : {
                            _id : 0,
                            'name' : '_id',
                            'age' : '$ageAndDescription.age',
                            'description' : '$ageAndDescription.description',
                            'total_name' : 1
                            }

db.collection.aggregate(term1group, term2unwind, term3project);

Haven't tested but i am hopeful this will work. 还没有测试过,但我希望它能起作用。

You just need to use a $group and $sum to do a simple count. 您只需要使用$group$sum进行简单计数即可。 The output won't match exactly, but you could reformat it with NodeJS easily. 输出不会完全匹配,但是您可以使用NodeJS轻松重新格式化。

You apparently want to group on the three fields shown ( name , age , and description ). 您显然想对显示的三个字段( nameagedescription )进行分组。 To do that, just add the fields and a field reference (using $ ): 为此,只需添加字段和字段引用(使用$ ):

  { $match: { name: 'andy' } },
  { $group: { 
         _id: { name: "$name", age: "$age", description: "$description"}, 
         count: { $sum: 1} 
       }
  }

To add the count of each group, include a $sum of 1 (for each document that matches the group). 要添加每个组的计数,请添加$sum 1 (对于与该组匹配的每个文档)。

Your output will look something like: 您的输出将类似于:

{ "_id" : { "name" : "andy", "age" : 17, "description" : "ccc ccc ccc" }, "count" : 1 }
{ "_id" : { "name" : "andy", "age" : 17, "description" : "bbb bbb bbb" }, "count" : 1 }
{ "_id" : { "name" : "andy", "age" : 19, "description" : "aaa aaa aaa" }, "count" : 3 }

If you used a projection with $project , you could also format the output to more closely match your original request: 如果您将投影与$project ,则还可以设置输出格式,使其与原始请求更加匹配:

{ $match: {name: 'andy' }}, 
{ $group: { _id: { name: "$name", age: "$age", description: "$description"} , 
         count: {$sum: 1}}
}, 
{ $project : { name: "$_id.name", _id: 0, age: "$_id.age", 
              description: "$_id.description", total_andy: "$count" 
             }
}

Results: 结果:

{ "name" : "andy", "age" : 17, "description" : "ccc ccc ccc", "total_andy" : 1 }
{ "name" : "andy", "age" : 17, "description" : "bbb bbb bbb", "total_andy" : 1 }
{ "name" : "andy", "age" : 19, "description" : "aaa aaa aaa", "total_andy" : 3 }

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

相关问题 如何对MongoDB中的聚合查询结果分页并得到总文档数(Node.js + Mongoose)? - How to paginate aggregation query results in MongoDB and get a total document count (Node.js + Mongoose)? MongoDB 使用 $search 获取总计数聚合管道 - MongoDB get total count aggregation pipeline with $search 获取mongodb中的文档总数 - Get total number of documents in mongodb MongoDB 聚合:如何同时获取组_id 和总记录数? - MongoDB Aggregation: How to get both group _id and total records count? 使用限制时使用 MongoDB 聚合获取总文档数 - Get a count of total documents with MongoDB aggregation when using limit Mongodb全文搜索,获取文档中匹配的字符串 - Mongodb Full text search, get the matched string in the document MongoDB 聚合查询,以每年-月返回集合中的条目总数,按位置值分组 - MongoDB aggregation query to return total number of entries in a collection per year-month, grouped by a location value 使用mongodb聚合将子文档合并到主文档 - Merge a sub document to main document with mongodb aggregation 使用聚合查询获取具有总交易数和交易明细的用户列表作为嵌入式文档 - Using aggregation query to get list of users with total transaction count and transaction detail as embedded document Mongodb 聚合以传递匹配数组和不匹配数组 - Mongodb aggregation to pass both a matched array and an unmatched array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM