@@ -214,18 +214,17 @@ def _gen_get_more_command(cursor_id, coll, batch_size, max_await_time_ms):
214
214
class _Query (object ):
215
215
"""A query operation."""
216
216
217
- __slots__ = ('flags' , 'db' , 'coll' , 'ntoskip' , 'ntoreturn' , ' spec' ,
217
+ __slots__ = ('flags' , 'db' , 'coll' , 'ntoskip' , 'spec' ,
218
218
'fields' , 'codec_options' , 'read_preference' , 'limit' ,
219
219
'batch_size' , 'name' , 'read_concern' )
220
220
221
- def __init__ (self , flags , db , coll , ntoskip , ntoreturn , spec , fields ,
221
+ def __init__ (self , flags , db , coll , ntoskip , spec , fields ,
222
222
codec_options , read_preference , limit ,
223
223
batch_size , read_concern ):
224
224
self .flags = flags
225
225
self .db = db
226
226
self .coll = coll
227
227
self .ntoskip = ntoskip
228
- self .ntoreturn = ntoreturn
229
228
self .spec = spec
230
229
self .fields = fields
231
230
self .codec_options = codec_options
@@ -260,12 +259,21 @@ def get_message(self, set_slave_ok, is_mongos, use_cmd=False):
260
259
261
260
ns = _UJOIN % (self .db , self .coll )
262
261
spec = self .spec
263
- ntoreturn = self .ntoreturn
264
262
265
263
if use_cmd :
266
264
ns = _UJOIN % (self .db , "$cmd" )
267
265
spec = self .as_command ()[0 ]
268
266
ntoreturn = - 1 # All DB commands return 1 document
267
+ else :
268
+ # OP_QUERY treats ntoreturn of -1 and 1 the same, return
269
+ # one document and close the cursor. We have to use 2 for
270
+ # batch size if 1 is specified.
271
+ ntoreturn = self .batch_size == 1 and 2 or self .batch_size
272
+ if self .limit :
273
+ if ntoreturn :
274
+ ntoreturn = min (self .limit , ntoreturn )
275
+ else :
276
+ ntoreturn = self .limit
269
277
270
278
if is_mongos :
271
279
spec = _maybe_add_read_preference (spec ,
0 commit comments