I have 2 collections, a parent and a child collection.
The parent knows which children are linked.
The parent:
{
_id: ObejctId;
linkedChildren: ObjectId[]
}
I now wanna find the most efficient way, to find the children which are not linked in any parent.
My current query:
[
{
$lookup: {
from: 'parent',
localField: '_id',
foreignField: 'linkedChildren',
as: 'result',
},
},
{
$match: {
result: {
$size: 0,
},
},
},
{
$project: {
_id: 1,
},
},
]
How can I improve this and make it more efficient?
2
Answers
It has been a while since I lasted worked with Mongo, but I don’t think you can escape querying every item in your database.
If you want to improve your performance and don’t want to include parent logic in the child element, you could perhaps have a collection of "Orphan" elements. The downside is that you’d have to maintain that collection, but then you wouldn’t need the COLSCAN anymore.
If the collections are not too large, this aggregation pipeline gets all
"_id"
s from the children collection, all"linkedChildren"
"_id"
s, and then takes the"$setDifference"
to determine"kidsWithNoParent"
.N.B.: If either collection is too large, the
"$group"
stages will exhaust allowable memory.Try it on mongoplayground.net.