I am just getting started with MongoDB and trying to understand how indexes work. I have a list of items in a collection. Each item has a version that gets incremented. Then, all previous versions (less than current version) get removed (record is not updated so that both versions are available for a while). There is a compound index on item ID and version. For removing, does it make a difference (in terms of performance) whether you use $ne versus $lt?
I would assume no, but I just want to confirm.
Without knowing the details of the implementation
$ltcan be more efficient than$ne. On a B-tree index$newould be two range scans ($ltand$gt), whereas$ltis just one.But in your case
$ltseems to be what you want anyway (to find the older versions). If you used$ne, you could accidentally also remove newer versions that you just assume do not exist, but might actually have been created in the mean-time. Remember that MongoDB does not support transactions or consistent views across documents. Concurrent updates might bite you here.