使用集合B

时间:2019-02-08 09:39:12

标签: mongodb-query aggregation-framework

给出了以下2个合集歌曲和play_log

我需要一个具有查找功能的mongo聚合查询来完成操作 以获得Bowie的“ Scary Monsters”专辑中所有歌曲的播放日志以及与歌曲相关的信息。

收藏歌曲

[
    {artist: 'David Bowie', title: 'Ashes to Ashes', album: 'Scary Monsters', year:'1980', track_number: 4 ,label: 'RCA Records'},
    {artist: 'David Bowie', title: 'Fashion', album: 'Scary Monsters', year:'1980', track_number: 5 ,label: 'RCA Records'},
    ....
    {artist: 'U2', title: 'Sunday Bloody Sunday', album: 'war', year '1983', track_number: 1, label: 'Island Records'},
    {artist: 'U2', title: 'New Year's Day', album: 'war', year '1983', track_number: 3, label: 'Island Records'},
    {artist: 'U2', title: 'The Refugee', album: 'war', year '1983', track_number: 6, label: 'Island Records'},
    ....
]

收藏play_log

[
    { created: '2019-02-08T11:05:33', station: 'BBC Radio 6', artist: 'David Bowie', title: 'Ashes to Ashes' },
    { created: '2019-01-17T01:33:57', station: 'BBC Radio 1', artist: 'U2', title: 'Sunday Bloody Sunday' },
    { created: '2018-09-08T12:21:32', station: 'BBC Radio 2', artist: 'Morrissey', title: 'Every day is like Sunday' },
    { created: '2019-02-08T11:11:11', station: 'BBC Radio 4', artist: 'David Bowie', title: 'Fashion' },
    ...
]

预期结果

[
    { created: '2019-02-08T11:05:33', station: 'BBC Radio 6', artist: 'David Bowie', title: 'Ashes to Ashes', album:'Scary Monsters', year:'1980', track_number: 4 ,label: 'RCA Records'},
    { created: '2019-02-08T11:11:11', station: 'BBC Radio 4', artist: 'David Bowie', title: 'Fashion', album: 'Scary Monsters', year:'1980', track_number: 5 ,label: 'RCA Records'},
    ...
]

1 个答案:

答案 0 :(得分:0)

这可以通过使用$lookup运算符进行聚合来完成,如下所示:

db.play_log.aggregate([
// Join using artist fields
   {
     $lookup:
       {
         from: "songs",
         localField: "artist",
         foreignField: "artist",
         as: "play_songs_logs"
       }
  },
  // Filter any empty array found in newly created collection: play_songs_logs
  {
      $match: { "play_songs_logs": { $ne: [] } }
   }, 
   // Match only required album, this can be done before filter also if make aggregate on songs collection
  {
  $match: 
      {
          "play_songs_logs.album" : "Scary Monsters"
      }
  },
  // Push all elements or merged the elements
  {
      $replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$play_songs_logs", 0 ] }, "$$ROOT" ] } }
   },
   // Filter not required fields
   { $project: { play_songs_logs: 0, _id:0 } }
])

以上查询的输出:

{ "artist" : "David Bowie", "title" : "Ashes to Ashes", "album" : "Scary Monsters", "year" : "1980", "track_number" : 4, "label" : "RCA Records", "created" : "2019-02-08T11:05:33", "station" : "BBC Radio 6" }
{ "artist" : "David Bowie", "title" : "Fashion", "album" : "Scary Monsters", "year" : "1980", "track_number" : 4, "label" : "RCA Records", "created" : "2019-02-08T11:11:11", "station" : "BBC Radio 4" }

在执行上述查询之前,我已插入提供的数据并查找查询

> db.play_log.find()
{ "_id" : ObjectId("5c5f8c561765cd7b27eb4731"), "created" : "2019-02-08T11:05:33", "station" : "BBC Radio 6", "artist" : "David Bowie", "title" : "Ashes to Ashes" }
{ "_id" : ObjectId("5c5f8c561765cd7b27eb4732"), "created" : "2019-01-17T01:33:57", "station" : "BBC Radio 1", "artist" : "U2", "title" : "Sunday Bloody Sunday" }
{ "_id" : ObjectId("5c5f8c561765cd7b27eb4733"), "created" : "2018-09-08T12:21:32", "station" : "BBC Radio 2", "artist" : "Morrissey", "title" : "Every day is like Sunday" }
{ "_id" : ObjectId("5c5f8c561765cd7b27eb4734"), "created" : "2019-02-08T11:11:11", "station" : "BBC Radio 4", "artist" : "David Bowie", "title" : "Fashion" }
>

> db.songs.find()
{ "_id" : ObjectId("5c5f8c961765cd7b27eb4735"), "artist" : "David Bowie", "title" : "Ashes to Ashes", "album" : "Scary Monsters", "year" : "1980", "track_number" : 4, "label" : "RCA Records" }
{ "_id" : ObjectId("5c5f8c961765cd7b27eb4736"), "artist" : "David Bowie", "title" : "Fashion", "album" : "Scary Monsters", "year" : "1980", "track_number" : 5, "label" : "RCA Records" }
{ "_id" : ObjectId("5c5f8c961765cd7b27eb4737"), "artist" : "U2", "title" : "Sunday Bloody Sunday", "album" : "war", "year" : "1983", "track_number" : 1, "label" : "Island Records" }
{ "_id" : ObjectId("5c5f8c961765cd7b27eb4738"), "artist" : "U2", "title" : "New Year's Day", "album" : "war", "year" : "1983", "track_number" : 3, "label" : "Island Records" }
{ "_id" : ObjectId("5c5f8c961765cd7b27eb4739"), "artist" : "U2", "title" : "The Refugee", "album" : "war", "year" : "1983", "track_number" : 6, "label" : "Island Records" }
>

有关详细信息,请参阅此处的官方文档:https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/