聚合返回一个空数组
我一直在使用 mongoose odm 和 nodejs 环境,在我的开发过程中,我遇到了这样的问题。我需要获取文档列表,并对这些文档执行一些汇总操作。为了获取特定文档,我使用了 $匹配聚集,但它行不通, 我试图在没有$匹配阶段的情况下使用,所以它起作用(它返回了一些文档), 但是我必须指定确切的字段,并且使用了匹配项,但是它返回一个空数组。 我的管道下面(使用猫鼬):
const comments = await Comments.aggregate([
{
'$match': {
'article_id': Types.ObjectId(id)
}
},
{
'$unwind':
{
path: '$replies'
}
},
{
'$lookup': {
from: 'users',
localField: 'replies.sender',
foreignField: '_id',
'pipeline': [
{'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
],
as: 'replies.sender'
}
},
{
'$unwind': {
path: '$replies.sender'
}
},
{
'$group': {
'_id': '$_id',
'replies': {
'$push': '$replies'
}
}
},
{
'$lookup': {
from: 'comments',
localField: '_id',
foreignField: '_id',
as: 'commentDetails'
}
},
{
'$unwind': {
path: '$commentDetails'
}
},
{
'$addFields': {
'commentDetails.replies': '$replies'
}
},
{
'$replaceRoot': {
'newRoot': '$commentDetails'
}
},
{
'$unwind':
{
path: '$replies'
}
},
{
'$lookup': {
from: 'users',
localField: 'replies.receiver',
foreignField: '_id',
'pipeline': [
{'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
],
as: 'replies.receiver'
}
},
{
'$unwind': {
path: '$replies.receiver'
}
},
{
'$group': {
'_id': '$_id',
'replies': {
'$push': '$replies'
}
}
},
{
'$lookup': {
from: 'comments',
localField: '_id',
foreignField: '_id',
as: 'commentDetails'
}
},
{
'$unwind': {
path: '$commentDetails'
}
},
{
'$addFields': {
'commentDetails.replies': '$replies'
}
},
{
'$replaceRoot': {
'newRoot': '$commentDetails'
}
},
{
'$lookup': {
'from': 'users',
'localField': 'user_id',
'foreignField': '_id',
'pipeline': [
{'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
],
'as': 'user'
}
},
{
'$unwind': '$user'
},
]).sort({'date_time': 'descending'})
I have been using mongoose ODM with Nodejs environment, during my development I faced such problem. I need to get a list of docs and perform some aggregation operations on these docs. In order to get specific docs I used $match aggregation, but it does not work,
I have tried to use without $match stage, so it worked (it returned some docs),
but I have to specify exact field and I used match, but it returns an empty array.
My pipeline below (using mongoose):
const comments = await Comments.aggregate([
{
'$match': {
'article_id': Types.ObjectId(id)
}
},
{
'$unwind':
{
path: '$replies'
}
},
{
'$lookup': {
from: 'users',
localField: 'replies.sender',
foreignField: '_id',
'pipeline': [
{'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
],
as: 'replies.sender'
}
},
{
'$unwind': {
path: '$replies.sender'
}
},
{
'$group': {
'_id': '$_id',
'replies': {
'$push': '$replies'
}
}
},
{
'$lookup': {
from: 'comments',
localField: '_id',
foreignField: '_id',
as: 'commentDetails'
}
},
{
'$unwind': {
path: '$commentDetails'
}
},
{
'$addFields': {
'commentDetails.replies': '$replies'
}
},
{
'$replaceRoot': {
'newRoot': '$commentDetails'
}
},
{
'$unwind':
{
path: '$replies'
}
},
{
'$lookup': {
from: 'users',
localField: 'replies.receiver',
foreignField: '_id',
'pipeline': [
{'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
],
as: 'replies.receiver'
}
},
{
'$unwind': {
path: '$replies.receiver'
}
},
{
'$group': {
'_id': '$_id',
'replies': {
'$push': '$replies'
}
}
},
{
'$lookup': {
from: 'comments',
localField: '_id',
foreignField: '_id',
as: 'commentDetails'
}
},
{
'$unwind': {
path: '$commentDetails'
}
},
{
'$addFields': {
'commentDetails.replies': '$replies'
}
},
{
'$replaceRoot': {
'newRoot': '$commentDetails'
}
},
{
'$lookup': {
'from': 'users',
'localField': 'user_id',
'foreignField': '_id',
'pipeline': [
{'$project': {'first_name': 1, 'last_name': 1, '_id': 0}}
],
'as': 'user'
}
},
{
'$unwind': '$user'
},
]).sort({'date_time': 'descending'})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方案。我刚刚添加了
preservenullandemptyarrays:true
<代码> $ undindI found a solution. I just added
preserveNullAndEmptyArrays: true
to$unwind