Search code examples
mongodbmongoosequery-performance

How specific should I be in MongoDB update commands, for performance?


I'm curious as to what should be specified in the where query when performing an updateMany command with mongoose.

For example given this update operation: { $pull: { tags: { $in: ["tagone", "tagtwo"] } } }

Should the where query be:

{organisation: "fffffff..."} or {organisation: "fffffff...", tags: {$in: ["tagone", "tagtwo"]}

Would the extra specificity in the where query improve or reduce performance, or make no difference?

(No database indexes apply)


Solution

  • I would put few things what I know, Please update this if there are any enhancements :

    1) Ok think of it this way if you don't have a filter specified & no indexes - then query will iterate through all the documents in the collection look into tags array for the values passed in input array & pull those values if they exist in tags. Every documents tags array has to be searched for values.

    2) If you've a filter - Even then since you don't have any indexes enabled, query has to scan through all the documents in the collection which is a collection scan, & then it would iterate through the filtered documents(matched documents from the filter provided) & update tags array.

    Why do we need indexes in collection & filters in query ?

    First of all why would you wanted to do it with no indexes applied ? Every collection must be properly indexed.

    Iterating on index keys(values of indexed field) in memory & fetch the actual documents from disk, update those & write it back would be fast using indexes, On the other hand having filters will let query run through less documents or index keys(if indexed) to process any updates or in case of aggregation for any transformations of the document in further stages.