我目前有三个集合:artists
、albums
和users
。每个用户都有一个他“喜欢”的相册列表,我只是尝试设置一个查询(或聚合管道),该查询将返回一个布尔值,无论特定用户是否喜欢相册。这些收藏看起来是这样的:
艺术家
{
"_id" : ObjectId("5e2b5c922e1843a8ed16df3b"),
"name" : "Death Grips",
"formed" : "21 Dec 2010",
"members" : [
"Stefan Burnett",
"Zach Hill",
"Andy Morin"
],
"based_in" : "Sacramento, CS, United States",
"genres" : [
"Experimental Hip Hop",
"Industrial Hip Hop",
"Abstract Hip Hop",
"Hardcore Hip Hop",
"Glitch Hop",
"Noise Rock"
]
}
相册
{
"_id" : ObjectId("5e2c41a4dce5f62fb2488ed8"),
"artist" : ObjectId("5e2b5c922e1843a8ed16df3b"),
"title" : "The Money Store",
"release_date" : "24 April 2012",
"image" : "5e2b9fd5b9be486f427f8331.jpg",
"rating" : 3.96,
"genres" : [
"Experimental Hip Hop",
"Industrial Hip Hop",
"Abstract Hip Hop",
"Hardcore Hip Hop",
"Glitch Hop",
"Noise Rock"
],
"descriptors" : [
"aggressive",
"angry",
"energetic",
"manic",
"noisy",
"nihilistic"
],
"lang" : "English",
"track_listing" : [
"Get Got",
"The Fever",
"Lost Boys",
"Black Jack",
"Hustle Bones",
"I've Seen Footage",
"Double Helix",
"System Blower",
"The Cage",
"Punk Weight",
"Fuck That",
"Bitch Please",
"Hacker"
]
}
使用者
{
"_id" : ObjectId("5e2c610157053b19fb28ee4e"),
"username" : "stackoverflow_user",
"liked_albums" : [
"5e2c41a4dce5f62fb2488ed8"
]
}
这就是我想做的:
albums = col.aggregate([
{
"$lookup":{
"from": "artists",
"localField": "artist",
"foreignField": "_id",
"as": "artist"
}
}, ...something that would return true
if the a user with a particular id
likes that album.
这是我希望通过用户ObjectId("5e2c610157053b19fb28ee4e")
实现的近似返回查询:
{
"_id" : ObjectId("5e2c41a4dce5f62fb2488ed8"),
"artist" : {
"_id" : ObjectId("5e2b5c922e1843a8ed16df3b"),
"name" : "Death Grips",
"formed" : "21 Dec 2010",
"members" : [
"Stefan Burnett",
"Zach Hill",
"Andy Morin"
],
"based_in" : "Sacramento, CS, United States",
"genres" : [
"Experimental Hip Hop",
"Industrial Hip Hop",
"Abstract Hip Hop",
"Hardcore Hip Hop",
"Glitch Hop",
"Noise Rock"
]
},
"title" : "The Money Store",
"release_date" : "24 April 2012",
"image" : "5e2b9fd5b9be486f427f8331.jpg",
"rating" : 3.96,
"genres" : [
"Experimental Hip Hop",
"Industrial Hip Hop",
"Abstract Hip Hop",
"Hardcore Hip Hop",
"Glitch Hop",
"Noise Rock"
],
"descriptors" : [
"aggressive",
"angry",
"energetic",
"manic",
"noisy",
"nihilistic"
],
"lang" : "English",
"track_listing" : [
"Get Got",
"The Fever",
"Lost Boys",
"Black Jack",
"Hustle Bones",
"I've Seen Footage",
"Double Helix",
"System Blower",
"The Cage",
"Punk Weight",
"Fuck That",
"Bitch Please",
"Hacker"
],
"liked": true
}
谢谢
此解决方案是否满足您的要求
在最后一个阶段,我们迭代所有喜欢这张专辑的用户。 将
$filter
条件更改为筛选用户_id
MongoPlayground
相关问题 更多 >
编程相关推荐