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