I have collection of documents which has the following shape:
const categorySchema = new Schema<ICategory>({
title: { type: String, required: true },
children: [{ type: Types.ObjectId, ref: categoryModelName }],
});
I want to get all documents id of which is not contained in children array property of all other documents in the collection. How to write such query, if possible?
2
Answers
you can write an aggregation pipeline
playground
Here’s another way to do it, although if the collection is very large the
"$group"
stage may exceed the max allowable memory. Comments are in the aggregation pipeline.Try it on mongoplayground.net.