$lookup 为子数组中的每个元素 [英] $lookup for each element within a sub-array
问题描述
所以在我的数据库中,我有 3 个集合,它们看起来像这样:
So in my Database, I have the 3 collections, and they look like this:
客户:
customers = [
{_id: 1, username: "jack", ... },
{_id: 2, username: "jane", ... }
...
]
评论:
reviews = [
{ _id: 1, customerID: 1, message: "my message", ...}
...
]
评论:
comments = [
{ _id: 1, reviewID: 1, customerID: 2, message: "my response" ...}
...
]
客户可以发表评论,也可以评论其他评论.所以,我想要的是一个 mongodb aggregation
查询:
Customers can post reviews, and can also comment on other reviews.
So, what I want is a mongodb aggregation
query to:
检索评论.
Retrieve the reviews.
进行评论的客户的数据.
The data of the customer who made the review.
对该评论的评论.
对该评论发表评论的客户的数据.
The data of the customers who made the comment on that review.
即
reviews = [
{
_id: 1,
username: "jack",
message: "my message"
comments: [
{ _id: 1, username: "jane", message: "my response", ...},
...
]
...
}
...
]
推荐答案
您可以从 comments
集合和 $lookup with customers
得到 customer
名字,然后你就可以$group 评论和 $lookup 两次(使用 reviews
和 customer
).每次你知道这是一对一的关系时,你可以使用 $unwind 在 $lookup 之后.试试:
You can start from comments
collection and $lookup with customers
to get customer
name, then you can $group all comments by review and $lookup twice (with reviews
and customer
). Every time you know that it's a one-to-one relationship you can use $unwind after $lookup. Try:
db.comments.aggregate([
{
$lookup: {
from: "customers",
localField: "customerID",
foreignField: "_id",
as: "customer"
}
},
{
$unwind: "$customer"
},
{
$project: {
_id: 1,
reviewID: 1,
username: "$customer.username",
message: 1
}
},
{
$group: {
_id: "$reviewID",
comments: { $push: { _id: "$_id", username: "$username", message: "$message" } }
}
},
{
$lookup: {
from: "reviews",
localField: "_id",
foreignField: "_id",
as: "review"
}
},
{
$unwind: "$review"
},
{
$lookup: {
from: "customers",
localField: "review.customerID",
foreignField: "_id",
as: "customer"
}
},
{
$unwind: "$customer"
},
{
$project: {
_id: 1,
message: "$review.message",
username: "$customer.username",
comments: 1
}
}
])
输出:
{ "_id" : 1, "comments" : [ { "_id" : 1, "username" : "jane", "message" : "my response" } ], "message" : "my message", "username" : "jack" }
如果您想从 reviews
开始并将其过滤为单个电影,则可以使用 $lookup 与自定义管道
If you want to start from reviews
and filter it out to single movie you can then you can use $lookup with custom pipeline
db.reviews.aggregate([
{
$match: {
movieId: 1,
}
},
{
$lookup: {
from: "customers",
localField: "customerID",
foreignField: "_id",
as: "customer"
}
},
{
$unwind: "$customer"
},
{
$lookup: {
from: "comments",
let: { reviewId: "$_id" },
pipeline: [
{
$match: { $expr: { $eq: [ "$$reviewId", "$reviewID" ] } }
},
{
$lookup: {
from: "customers",
localField: "customerID",
foreignField: "_id",
as: "customer"
}
},
{
$unwind: "$customer"
},
{
$project: {
_id: 1,
message: 1,
username: "$customer.username"
}
}
],
as: "comments"
}
},
{
$project: {
_id: 1,
message: 1,
username: "$customer.username",
comments: 1
}
}
])
带来相同的输出
这篇关于$lookup 为子数组中的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!