$ lookup mongodb中的嵌套数组

编程入门 行业动态 更新时间:2024-10-28 07:22:00
本文介绍了$ lookup mongodb中的嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在与MongoDB中新的(可爱的)lookup运算符作斗争.我有3个收藏集:

I am struggling with the newish (lovely) lookup operator in MongoDB. I have 3 collections:

艺术家

{ "_id" : ObjectId("5b0d2b2c7ac4792df69a9942"), "name" : "Dream Theater", "started_in" : NumberInt(1985), "active" : true, "country" : "US", "current_members" : [ ObjectId("5b0d2a7c7ac4792df69a9941") ], "previous_members" : [ ObjectId("5b0d2bf57ac4792df69a9954") ], "albums" : [ ObjectId("5b0d16ee7ac4792df69a9924"), ObjectId("5b0d47667ac4792df69a9994") ], "genres" : [ "prog metal", "prog rock" ] }

相册

{ "_id" : ObjectId("5b0d16ee7ac4792df69a9924"), "title" : "Images and words", "released" : ISODate("1992-07-07T00:00:00.000+0000"), "songs" : [ ObjectId("5b0d15ab7ac4792df69a9916"), ObjectId("5b0d15ee7ac4792df69a991e"), ObjectId("5b0d2db37ac4792df69a995d"), ObjectId("5b0d2dbe7ac4792df69a995e"), ObjectId("5b0d2dcb7ac4792df69a995f"), ObjectId("5b0d2dd87ac4792df69a9960"), ObjectId("5b0d2de27ac4792df69a9961"), ObjectId("5b0d2dec7ac4792df69a9962") ], "type" : "LP" } { "title" : "Awake", "released" : ISODate("1994-10-04T00:00:00.000+0000"), "songs" : [ ObjectId("5b0d470d7ac4792df69a9991") ], "type" : "LP", "_id" : ObjectId("5b0d47667ac4792df69a9994") }

歌曲

{ "_id" : ObjectId("5b0d15ab7ac4792df69a9916"), "title" : "Pull me under" } { "_id" : ObjectId("5b0d15ee7ac4792df69a991e"), "title" : "Another day" } { "title" : "Take the time", "_id" : ObjectId("5b0d2db37ac4792df69a995d") } { "title" : "Surrounded", "_id" : ObjectId("5b0d2dbe7ac4792df69a995e") } { "title" : "Metropolis - part I", "_id" : ObjectId("5b0d2dcb7ac4792df69a995f") } { "title" : "Under a glass moon", "_id" : ObjectId("5b0d2dd87ac4792df69a9960") } { "title" : "Wait for sleep", "_id" : ObjectId("5b0d2de27ac4792df69a9961") } { "title" : "Learning to live", "_id" : ObjectId("5b0d2dec7ac4792df69a9962") } { "title" : "6:00", "_id" : ObjectId("5b0d470d7ac4792df69a9991") }

我可以轻松地使用$lookup进行聚合以获取详细的albums数组,但是如何在相应的相册中获取详细的songs呢? 我想扩展以下查询:

I can easily do an aggregation with $lookup to get the detailed albums array, but how do I get also the detailed songs in the corresponding albums? I would like to extend the following query:

db.artists.aggregate([ { $lookup: { from: "albums", localField: "albums", foreignField: "_id", as: "albums" } }]).pretty()

推荐答案

如果您的mongodb版本为 3.6 ,则可以尝试使用嵌套的 $lookup 聚合...

If you have mongodb version 3.6 then you can try with nested $lookup aggregation...

db.collection.aggregate([ { "$lookup": { "from": Albums.collection.name, "let": { "albums": "$albums" }, "pipeline": [ { "$match": { "$expr": { "$in": [ "$_id", "$$albums" ] } } }, { "$lookup": { "from": Songs.collection.name, "let": { "songs": "$songs" }, "pipeline": [ { "$match": { "$expr": { "$in": [ "$_id", "$$songs" ] } } } ], "as": "songs" }} ], "as": "albums" }} ])

对于冗长的解释,您可以通过 $ lookup多个级别而无需$ unwind?

And for long-winded explanation you can go through $lookup multiple levels without $unwind?

或者如果您的Mongodb版本低于 3.6

Or If you have mongodb version prior to 3.6

db.collection.aggregate([ { "$lookup": { "from": Albums.collection.name, "localField": "albums", "foreignField": "_id", "as": "albums" }}, { "$unwind": "$albums" }, { "$lookup": { "from": Songs.collection.name, "localField": "albums.songs", "foreignField": "_id", "as": "albums.songs", }}, { "$group": { "_id": "$_id", "name": { "$first": "$name" }, "started_in": { "$first": "$started_in" }, "active": { "$first": "$active" }, "country": { "$first": "$country" }, "albums": { "$push": { "_id": "$albums._id", "title": "$albums.title", "released": "$albums.released", "type": "$albums.type", "songs": "$albums.songs" } } }} ])

更多推荐

$ lookup mongodb中的嵌套数组

本文发布于:2023-11-22 15:27:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数组   lookup   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!