Obtaining $group result with group count

后端 未结 2 1700
借酒劲吻你
借酒劲吻你 2020-12-14 16:20

Assuming I have a collection called \"posts\" (in reality it is a more complex collection, posts is too simple) with the following structure:

> db.posts.f         


        
2条回答
  •  萌比男神i
    2020-12-14 16:57

    I'm not sure you need the aggregation framework for this other than counting all the tags eg:

    db.posts.aggregate(
      { "unwind" : "$tags" },
      { "group" : {
          _id: { tag: "$tags" },
          count: { $sum: 1 }
      } }
    );
    

    For paginating through per tag you can just use the normal query syntax - like so:

    db.posts.find({tags: "RANDOM"}).skip(10).limit(10)
    

提交回复
热议问题