MongoDB按数组字段中的值分组

MongoDB按数组字段中的值分组,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有以下格式的资产文件 db.asset.find({}).limit(1).pretty() { "_id" : ObjectId("54e650a10364a65f62c0df4a"), "_class" : "com.model.core.Asset", "displayName" : "Bingo Rhymes For Children + More 3D Animation Nursery Rhymes & Kids' Songs", "asse

我有以下格式的资产文件

db.asset.find({}).limit(1).pretty()
{
    "_id" : ObjectId("54e650a10364a65f62c0df4a"),
    "_class" : "com.model.core.Asset",
    "displayName" : "Bingo Rhymes For Children + More 3D Animation Nursery Rhymes & Kids' Songs",
    "assetType" : "VIDEO",
    "active" : true,
    "originalUrl" : "https://www.youtube.com/watch?v=j-tdVvvXn9k&feature=youtube_gdata",
    "groupIds" : [ ],
    "folderIds" : [
        "54e6507b0364a65f62c0df47",
        "54e6507b0364a65f62c0df48"
    ]
}

如您所见,每个资产都可以有一个与其关联的folderId集合。如果我想找到FolderId和相关资产,mongo聚合查询会是什么样子?基本上,我想按folderId对资产进行分组。

首先需要按
folderId
字段展开,而不是按
\u id
分组,并将资产
\u id
推送到列表
资产\u id

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

不清楚你在问什么。你想干什么?预期结果是什么?@user3100115本质上我想按folderId对资产进行分组。我想这会让我得到folderId与assetId的一对一关联。以下是您建议的查询结果。db.asset.aggregate([{$unwind:$folderIds},{$group:{$id:{“folderIds:{$folderIds”,“资产id:{$id:{$id:}}}]){{{id:{“folderIds:“54f8a2ed0364edbff0152df3”,“资产id:”ObjectId:“55a53cfd93861c7d709af3c7”)}{{“folderIds:“54F8A20364EDF0152DF3”,“资产id:”ObjectId:“55A53CFD9324C”)}。我可以为每个folderId将AssetID分组在一起吗?谢谢,但是新查询不会返回任何结果。谢谢。这很有效。聚合([{$unwind:$folderIds},{$group:{{{U id:$folderIds],资产:{$push:{AssetsUID:$U id}}}}}}])