我正在try 使用Mongoose的加法门函数从Mongoose模式中检索array.我对这个项目使用的是nestjs,但查询是相同的:
const notifications = await this.userRepo.aggregate( [
{ $match: { _id: userId } },
{ $unwind: "$notifications" },
{ $replaceRoot: { newRoot: '$notifications' } }
] );
这里的问题是,每次我得到一个空array.无论文档中包含什么内容.
以下是一个文档示例:
{
_id: new ObjectId("6471fe4a0045e48a0c6193ba"),
gateways: [
{
_id: '5231db1f-b79a-4dc3-a387-6f08ad7b6857',
name: 'Babylon Dev',
right: 'owner'
}
],
__v: 0,
notifications: [
{
type: 'confirm',
title: 'Gateway whitelist request!',
description: '6471fe4a0045e48a0c6193ba wants to share data from gateway 5231db1f-b79a-4dc3-a387-6f08ad7b6857 with you.',
actions: [Array],
from: '6471fe4a0045e48a0c6193ba',
subject: '5231db1f-b79a-4dc3-a387-6f08ad7b6857',
status: 'unread',
_id: new ObjectId("64b6791adbf7bb6eaa528cf2")
}
]
}
有没有人能告诉我,我把这个设在哪里错了?
我想要的结果是获得通知数组:
[
{
type: 'confirm',
title: 'Gateway whitelist request!',
description: '6471fe4a0045e48a0c6193ba wants to share data from gateway 5231db1f-b79a-4dc3-a387-6f08ad7b6857 with you.',
actions: [Array],
from: '6471fe4a0045e48a0c6193ba',
subject: '5231db1f-b79a-4dc3-a387-6f08ad7b6857',
status: 'unread',
_id: new ObjectId("64b6791adbf7bb6eaa528cf2")
}
]
此外,获取/更新/删除notifications
数组中的一个元素也会有所帮助.