how to group in mongoDB and return all fields in result

后端 未结 5 583
梦如初夏
梦如初夏 2020-12-30 04:07

I am using aggregate method in mongoDB to group but when I use $group it returns the only field which I used to group. I have tried $project but it

5条回答
  •  半阙折子戏
    2020-12-30 04:56

    You can use this query

    db.col.aggregate([
                            {"$group" : {"_id" : "$name","data" : {"$first" : "$$ROOT"}}},
                            {"$project" : {
                                "tags" : "$data.tags",
                                "name" : "$data.name",
                                "rating" : "$data.rating",
                                "_id" : "$data._id"
                                }
                            }])
    

提交回复
热议问题