如何使用MongoDB的Java驱动程序将许多文档合并为一个文档?

时间:2019-09-21 19:54:11

标签: java mongodb

我正在处理包含音乐播放列表的文档。

每个文档都具有以下结构:

{
    "user_id": "5858",
    "playlists": [
        {
            "name": "My Playlist",
            "guild_ids": ["7575"],
            "items": [
                {
                     "title": "title",
                     "url": "url",
                     "duration": 200000
                } 
            ]
        }
    ]
}

我想从同一个公会中提取所有播放列表。 但问题是我希望将结果返回到单个文档中。一个带有播放列表列表的文档。

guild_id = 5656的预期结果如下:

{
    "playlists": [
        {
            "name": "My Playlist",
            "guild_ids": ["5656"],
            "items": [
                {
                     "title": "title",
                     "url": "url",
                     "duration": 200000
                } 
            ]
        },
        // other playlists where guild_ids contains "5656"
    ]
}

我尝试使用聚合,但是我总是得到与唯一user_id相同数量的文档。我得到了按user_id分组的播放列表。

1 个答案:

答案 0 :(得分:0)

以下查询可以为我们提供预期的输出:

db.collection.aggregate([
    {
        $unwind:"$playlists"
    },
    {
        $match:{
            "playlists.guild_ids":{
                $in:["7575"]
            }
        }
    },
    {
        $group:{
            "_id":null,
            "playlists":{
               $push: "$playlists"
            }
        }
    },
    {
        $project:{
            "_id":0
        }
    }
]).pretty()

数据集:

{
  "_id" : ObjectId("5d88225e38db7cf8d3f75cd6"),
  "user_id" : "5858",
  "playlists" : [
    {
      "name" : "My Playlist",
      "guild_ids" : [
        "7575"
      ],
      "items" : [
        {
          "title" : "title",
          "url" : "url",
          "duration" : 200000
        }
      ]
    }
  ]
}
{
  "_id" : ObjectId("5d88225e38db7cf8d3f75cd7"),
  "user_id" : "5858",
  "playlists" : [
    {
      "name" : "My Playlist 2",
      "guild_ids" : [
        "1234"
      ],
      "items" : [
        {
          "title" : "title",
          "url" : "url",
          "duration" : 200000
        }
      ]
    }
  ]
}
{
  "_id" : ObjectId("5d88225e38db7cf8d3f75cd8"),
  "user_id" : "5858",
  "playlists" : [
    {
      "name" : "My Playlist 3",
      "guild_ids" : [
        "7575"
      ],
      "items" : [
        {
          "title" : "title",
          "url" : "url",
          "duration" : 200000
        }
      ]
    }
  ]
}

输出:

{
    "playlists" : [
        {
            "name" : "My Playlist",
            "guild_ids" : [
                "7575"
            ],
            "items" : [
                {
                    "title" : "title",
                    "url" : "url",
                    "duration" : 200000
                }
            ]
        },
        {
            "name" : "My Playlist 3",
            "guild_ids" : [
                "7575"
            ],
            "items" : [
                {
                    "title" : "title",
                    "url" : "url",
                    "duration" : 200000
                }
            ]
        }
    ]
}

查询分析:我们正在展开playlists,仅过滤具有7575公会ID的用户,然后将其重新分组。