[SERVER-11815] __getitem__ send a limit of -1 Created: 21/Nov/13  Updated: 06/Dec/22  Resolved: 06/Dec/18

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: 2.2.4, 2.4.8
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: xbsura Assignee: [DO NOT USE] Backlog - Sharding Team
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongodb 2.2.4


Issue Links:
Related
is related to JAVA-950 Wrong fetching with limit 1 on sharde... Closed
Assigned Teams:
Sharding
Operating System: ALL
Participants:

 Description   

db.collection.find().limit(-1) and db.collection.find().limit(1) is equivalent in mongo 2.4.7,but our mongo being used is 2.2.4.We found that sometimes limit(-1) is different with limit(1) when querying.
in pymongo,the function _getitem_ send a limit of -1 when getting one document:

if isinstance(index, (int, long)):
if index < 0:
raise IndexError("Cursor instances do not support negative"
"indices")
clone = self.clone()
clone.skip(index + self.__skip)
clone.limit(-1) # use a hard limit
for doc in clone:
return doc
raise IndexError("no such item for Cursor instance")
raise TypeError("index %r cannot be applied to Cursor "
"instances" % index)
In large skip,mongo return null,which is expected to return one document. After changing limit(-1) to limit(1),we got correct result. I consider limit(-1) is a bug,or does it exist something i did not realized?



 Comments   
Comment by Gregory McKeon (Inactive) [ 06/Dec/18 ]

Closing as it should have gone away with the find changes in 3.2 - if you still have this issue, please open a new SERVER ticket.

Comment by Jeffrey Yemin [ 11/Dec/13 ]

This has been reported again in JAVA-950 and I can reproduce it in the shell with a 2.4.8 sharded cluster.

Comment by A. Jesse Jiryu Davis [ 21/Nov/13 ]

This appears to be a bug in the 2.2 series of mongos that was fixed in the 2.4 series.

Comment by xbsura [ 21/Nov/13 ]

yes,in mongodb 2.2.4,the two querying is different sometime.
I can reproduce this behavior in my collection,but not all collections have such behavior.
mongos> db.x_000.find().skip(15000).limit(-1)
mongos> db.x_000.find().skip(15000).limit(1)

{ "_id" : ObjectId("52009c7bb78b270bf1f25f31"), "idx" : 25274, "random" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "name" : "testms_insert_25274" }

in some cases,limit(-1) and limit(1) return different results.

mongos> db.x_000.find().skip(14620).limit(-1)

{ "_id" : ObjectId("5203ca94b78b27064d8be140"), "idx" : 75042, "random" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "name" : "testms_insert_75042" }

mongos> db.x_000.find().skip(14620).limit(1)

{ "_id" : ObjectId("520497a4b78b2757cab83ff1"), "idx" : 25259, "random" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "name" : "testms_insert_25259" }

even sort({_id:1}),they are still different.
I dont know why.

Comment by A. Jesse Jiryu Davis [ 21/Nov/13 ]

There is a reason PyMongo sends a limit of -1 instead of 1. For small limits, a negative number is much more efficient. See the documentation for numberToReturn for more information.

It sounds like you're seeing a bug in the MongoDB server 2.2 series, that was fixed in the 2.4 series. Could you please try to reproduce it using the mongo shell, or another driver besides PyMongo?

You'll need to find a query for which PyMongo returns null every time with a limit of -1, and returns the correct document every time with a limit of 1. Then run the same query in the mongo shell:

> db.collection.find({ ... }).limit(1)
> db.collection.find({ ... }).limit(-1)

... and see if you can reproduce the incorrect behavior with MongoDB 2.2.4.

Generated at Thu Feb 08 03:26:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.