In a MongoDB query, what does a negative limit mean?

旧城冷巷雨未停 提交于 2019-12-22 01:50:34

问题


I'm using the Mongoid ruby gem to interact w/ MongoDB and when I try to get one of something from a query, it adds $limit: -1 (i.e. negative one) when I would expect it to just use 1. I tried doing the same thing in the console and it didn't change which document was returned.

Does a negative limit mean something special?


回答1:


There is a note on negative limits under the "numberToReturn" heading in the "OP_QUERY" section of the "Mongo Wire Protocol" documentation.

"If the client driver offers 'limit' functionality (like the SQL LIMIT keyword), then it is up to the client driver to ensure that no more than the specified number of document are returned to the calling application. If numberToReturn is 0, the db will used the default return size. If the number is negative, then the database will return that number and close the cursor. No further results for that query can be fetched."

For more information on cursors and limit, please see the "Queries and Cursors" documentation, specifically the now-removed "Execution of queries in batches" section.




回答2:


The sign of the limit value determines whether its a 'hard limit' or a 'soft limit'. A 'hard limit' (negative sign) query closes the cursor after it has returned the maximum number of documents it can. A 'soft limit' keeps the cursor open in case the response can not fit as many documents as specified by the limit.



来源:https://stackoverflow.com/questions/9833941/in-a-mongodb-query-what-does-a-negative-limit-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!