aggregate

 

# 统计orders集合数量
db.orders.aggregate( [
   { $group: { _id: null, count: { $sum: 1 } } },
   { $project: { _id: 0 } }
] )

 



db.articles.insertMany([
{
   _id: ObjectId("12769ea0f3dc6ead47c9a1b2"),
   author: "1",
   title: "zzz1",
   tags: [ "java", "database", "mongodb" ]
},
{
   _id: ObjectId("22769ea0f3dc6ead47c9a1b2"),
   author: "2",
   title: "zzz2",
   tags: [ "python", "database", "mongodb" ]
},
{
   _id: ObjectId("32769ea0f3dc6ead47c9a1b2"),
   author: "3",
   title: "zzz3",
   tags: [ "go", "mysql", "mongodb" ]
}
]);

db.articles.aggregate( [
   { $project: { tags: 1 } },
   { $unwind: "$tags" },
   { $group: { _id: "$tags", count: { $sum : 1 } } }
] )

 

aggregate_第1张图片

 

 

 

 

你可能感兴趣的:(mongodb)