mongotemplate aggregation with condition

后端 未结 2 703
孤街浪徒
孤街浪徒 2021-01-19 07:02

I have a collection where the documents look like this:

{
  _id: \"545b9fa0dd5318a4285f7ce7\",
  owner: \"admin\",  
  messages: [
    {
      id: \"100\",
          


        
2条回答
  •  醉梦人生
    2021-01-19 07:20

    You can use following aggregation :

    db.collection.aggregate(
        { $match : { "_id" : ObjectId("545b9fa0dd5318a4285f7ce7") } },
        { $unwind : "$messages" },
        { $group : { "_id" : "$messages.status", "count" : { $sum : 1} } }
    )
    

    it will give you the count of status for which message are available, all other status count should be consider 0.

提交回复
热议问题