Smart猫小萌
以下查询可以获得预期的输出: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的那些,然后将它们重新分组。