0

Context

Imagine that you have two MongoDB collections, one called nodes and the other called mirrors.

Each mirror is replica of a node. So, in case the node is removed (for whatever reason), you should delete its corresponding mirrors.

Currently, the query being performed to obtain mirrors whose node doesn't exist (so they should be deleted) is:

{
  '$lookup': {
    'from': 'nodes',
    'localField': 'node',
    'foreignField': '_id',
    'as': 'node'
  }
},
{
  '$match': {
    'node': {
      '$size': 0
    }
  }
}

This query performs well in an environment where mirrors' collection has 50 million or fewer documents (using a limit of around 500, of course).

As node and _id are indexed, this query could do the job, but as the size is increasing, it is starting to be so slow.

Question

Which could be the best solution to avoid or make this query faster? Could be a new collection where documents are the joined collections? Could be some sort of trick that I'm not realizing?

Please, take into count that these collections do not have other fields indexed neither I could index more fields as this is a very big database. The last time when I tried to index a field, mongo lasted 3 days to perform the change.

I'm looking for the best practices, but I can make some commitments as I know that this kind of case is not common in our logic, nor the database was thought for this type of operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.