У меня есть один документ для поста блога, подобный этому:
{
"_id": "5d8051cdf0b1017da7bff23c",
"description": "<p>will update soon</p>",
"topic": "How to setup Kafka Cluster on CentOS",
"comments": [
{
"created_at": "2019-10-12T02:13:01.859Z",
"updated_at": "2019-10-12T02:13:01.859Z",
"edited": false,
"_id": "5da182aa013d12567e340a2d",
"message": "test"
"replies": [
{
"created_at": "2019-10-12T02:13:01.859Z",
"updated_at": "2019-10-12T02:13:01.859Z",
"edited": false,
"_id": "5da182aa013d12567e340a2a",
"message": "I am replying to first comment",
"commentator": "5daae8b8af029ec4533fe317"
},
{
"created_at": "2019-10-12T02:13:01.859Z",
"updated_at": "2019-10-12T02:13:01.859Z",
"edited": false,
"_id": "5da182aa013d12567e340a2c",
"message": "Helpful second Comment",
"commentator": "5d7f936544dac213e3f650ec"
}
]
}
]
}
}
Я хочу выполнить вложенную агрегацию, используя mongodb.
На данный момент мой запрос
{ $unwind: { path: '$comments', preserveNullAndEmptyArrays: true } },
{ $unwind: { path: '$comments.replies', preserveNullAndEmptyArrays: true } },
{
$lookup: {
from: 'users',
let: { thread_reply_commentator: '$comments.replies.commentator' },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$thread_reply_commentator'] } } },
{ $project: AUTHOR_PROJECTION },
],
as: 'comments.replies.commentator'
}
},
{ $unwind: { path: '$comments.replies.commentator', preserveNullAndEmptyArrays: true } },
{
$group: {
_id: { _id: '$_id', comment: "$comments._id" },
root: { $mergeObjects: '$$ROOT' },
replies: { $push: '$comments.replies' }
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: ['$$ROOT.replies']
}
}
}
А теперь мой приведенный выше запрос приводит к
{
"_id": "5d8051cdf0b1017da7bff23c",
"description": "<p>will update soon</p>",
"topic": "How to setup Kafka Cluster on CentOS",
"comments": [
{
"_id": "5da182aa013d12567e340a2d",
"message": "test",
"replies": {
"created_at": "2019-10-12T02:13:01.859Z",
"updated_at": "2019-10-12T02:13:01.859Z",
"edited": false,
"_id": "5da182aa013d12567e340a2a",
"message": "I am replying to first comment",
"commentator": {
"first_name":"test",
"last_name":"test"
}
}
},
{
"_id": "5da182aa013d12567e340a2d",
"message": "test",
"replies": {
"created_at": "2019-10-12T02:13:01.859Z",
"updated_at": "2019-10-12T02:13:01.859Z",
"edited": false,
"_id": "5da182aa013d12567e340a2a",
"message": "Helpful second Comment",
"commentator": {
"first_name":"test",
"last_name":"test"
}
}
}
]
}
Но мой желаемый результат:
{
"_id": "5d8051cdf0b1017da7bff23c",
"description": "<p>will update soon</p>",
"topic": "How to setup Kafka Cluster on CentOS",
"comments": [
{
"_id": "5da182aa013d12567e340a2d",
"message": "test",
"replies": [
{
"created_at": "2019-10-12T02:13:01.859Z",
"updated_at": "2019-10-12T02:13:01.859Z",
"edited": false,
"_id": "5da182aa013d12567e340a2a",
"message": "I am replying to first comment",
"commentator": {
"first_name":"test",
"last_name":"test"
}
},
{
"created_at": "2019-10-12T02:13:01.859Z",
"updated_at": "2019-10-12T02:13:01.859Z",
"edited": false,
"_id": "5da182aa013d12567e340a2a",
"message": "Helpful second Comment",
"commentator": {
"first_name":"test",
"last_name":"test"
}
}
]
}
]
}
Пожалуйста, помогите, как мне этого добиться. Я знаю, что это легко возможно, используя mon goose, но у меня нет доступа к модели схемы, поэтому я могу использовать только mongodb и не могу использовать mon goose.