A simplified example of the data structure is
{ _id: ObjectId, parentId: ObjectId }
where a parent will have parentId
blank, and all the children will have parentId
point to the top level record that is in the same collection.
I have an Array of ID’s that can either be a parent or child ID and I need to retrieve all the parent records.
So if the ID is for a parent, cool all set.
If the given ID points to a record that has a parentId
, retrieve the record with the _id === parentId
.
Lastly, I can't have duplicates so if I have a child ID that points to a parent record that I already have (because I was given the ID explicitly), just take the unique parent record.
so I could have a query like:
query: { _id: {$in: [2,3,4]}}
with data like
_id | parentId
1 | undefined
2 | 1
3 | undefined
4 | 3
which should return the records for 1, 3
because 2
points to 1
, and although I have 3
and 4
, the top level is 3
.