开发者

Is there a difference between $lt/$gt and $ne in MongoDB?

I am just getting started with MongoDB and trying to understand how i开发者_如何学编程ndexes 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 $lt can be more efficient than $ne. On a B-tree index $ne would be two range scans ($lt and $gt), whereas $lt is just one.

But in your case $lt seems 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.


Actually, there's a huge difference. The "$ne and $nin operators are not selective", which means that an index will not speed up that part of the query at all. So if you use $ne, then the version part of the compound index will not be used by MongoDB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜