Answer the question
In order to leave comments, you need to log in
How to change the aggregate $group that returns null when $lookup is an empty array?
Good afternoon/evening,
Faced with sl. situation.
In general:
There is a collection of "recommendations", it has an entry sl. type:
{
"_id" : ObjectId("5c995106deafcc7c86b9f284"),
"id" : 777,
"name" : "developers",
"likesPeople" : [
{ "id" : 579 },
{ "id" : 398 }
]
}
db.getCollection('recommendations').aggregate([
{ $match: { id: 777 }},
{ $unwind: "$likesPeople" },
{ $lookup: {
from: "users",
localField: "likesPeople.id",
foreignField: "id",
as: "likesPeople"
} },
{ $unwind: "$likesPeople" },
{ $group : {
_id: '$_id',
likesPeople: { $push: "$likesPeople"},
id: { $first: '$id' }
}
}
])
{
"_id" : ObjectId("5c995106deafcc7c86b9f284"),
"id" : 777,
"likesPeople" : [
{
"_id" : ObjectId("5c99500adeafcc7c86b9f283"),
"id" : 579,
"name" : "Вася"
},
{
"_id" : ObjectId("5c99b429deafcc7c86b9f28b"),
"id" : 398,
"name" : "Маша"
}
]
}
{
"_id" : ObjectId("5c995106deafcc7c86b9f284"),
"id" : 777,
"name" : "developers",
"likesPeople" : []
}
{
"_id" : ObjectId("5c995106deafcc7c86b9f284"),
"id" : 777,
"name" : "developers",
"likesPeople" : null // или что угодно
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question