MongoDB group by values in an array field

后端 未结 1 486
梦谈多话
梦谈多话 2020-12-03 23:17

I have my asset documents in the below format.

db.asset.find({}).limit(1).pretty()
{
    \"_id\" : ObjectId(\"54e650a10364a65f62c0df4a\"),
    \"_class\" :          


        
1条回答
  •  Happy的楠姐
    2020-12-04 00:09

    You first need to unwind by the folderIds field, than group by _id and push the asset _id into a list assets_id.

    db.asset.aggregate([{$unwind:"$folderIds"},  {$group:{_id: "$folderIds",assets:{$push: {assets_id:"$_id"}}}}])
    

    0 讨论(0)
提交回复
热议问题