I'm linking two MongoDB collections, say A
and B
, by storing B's
ObjectId in A
(say A.bId
). How do I find all instances of A
, where bId
no longer valid (the corresponding B
doesn't exist)?
Based on answers and official documentation, it's clear that MongoDB doesn't enforce references. So I need an efficient way to find invalid A's
in order to purge them.
A
and B
are large collections, so I'd like to avoid:
- Loading all
B.id's
into memory and doing a$nin
- Iterating through all
A's
and doB.findById(A.bId)