Best way to search documents if array field does not exist in document


we have 3M records in collection. we want to populate array fields called “array1” and “array2” if doesn’t exist in document.
what would be the best way to make search faster for documents which don’t have array fields “array1” and “array2”. We can use “$exist” but it’s taking longer time


Hi @Dhruvesh_Patel

Try to create a partial index :

db.collection.createIndex( { _id : 1 }, { "partialFilterExpression": { "array1": { "$exists": false }, "array2": { "$exists": false } } } )

And do a batch update on the located _id from this index.

My gut feeling is that creating such an index would as slow as just doing the query for the update. I think that it could even be slower as the index has to be written to disk and then updated when the arrays are populated.

It may however distribute the work over a longer period and reduce the CPU spike.

But as with any performance issues, gut feeling is not always right.

It would be nice to know.