【发布时间】:2020-07-27 11:45:36
【问题描述】:
我的问题是我想对对象数组“评论”、“关注者”和“观看”的“作者”字段进行查找,但我不知道为什么它在其他对象中给了我这个结果数组,该值与“评论”数组中的文档重复相同的次数。
.unwind({ path: '$reviews', preserveNullAndEmptyArrays: true })
.lookup({
from: 'users',
let: { userId: '$reviews.author' },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
{
$project: {
name: 1,
username: 1,
photo: 1,
rank: 1,
'premium.status': 1,
online: 1,
},
},
],
as: 'reviews.author',
})
.unwind({ path: '$followers', preserveNullAndEmptyArrays: true })
.lookup({
from: 'users',
let: { userId: '$followers.author' },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
{
$project: {
name: 1,
username: 1,
photo: 1,
rank: 1,
'premium.status': 1,
online: 1,
},
},
],
as: 'followers.author',
})
.unwind({ path: '$watching', preserveNullAndEmptyArrays: true })
.lookup({
from: 'users',
let: { userId: '$watching.author' },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
{
$project: {
name: 1,
username: 1,
photo: 1,
rank: 1,
'premium.status': 1,
online: 1,
},
},
],
as: 'watching.author',
})
.group({
_id: '$_id',
data: {
$first: '$$ROOT',
},
reviews: {
$push: '$reviews',
},
followers: {
$push: '$followers',
},
watching: {
$push: '$watching',
},
})
这是“评论”有文档时的结果:
“Followers / Watching”数组在数据库中没有任何内容,但它在这里以这种方式显示,重复该值与评论中的文档数量相同,我不知道会发生什么。
当所有数组都为空时,会发生这种情况:
它一直显示,但我不知道如何修复它。
总而言之,“评论”,“观看”和“关注者”有一个“作者”字段,我想查找观看的作者字段,以及关注者和评论,但我有这些问题.请问我需要帮助。
示例:这是它在数据库中的外观:
非常感谢您。
【问题讨论】:
标签: mongodb join mongoose mongoose-populate