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?
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.
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.