@@ -214,18 +214,17 @@ def _gen_get_more_command(cursor_id, coll, batch_size, max_await_time_ms):
214214class _Query (object ):
215215 """A query operation."""
216216
217- __slots__ = ('flags' , 'db' , 'coll' , 'ntoskip' , 'ntoreturn' , ' spec' ,
217+ __slots__ = ('flags' , 'db' , 'coll' , 'ntoskip' , 'spec' ,
218218 'fields' , 'codec_options' , 'read_preference' , 'limit' ,
219219 'batch_size' , 'name' , 'read_concern' )
220220
221- def __init__ (self , flags , db , coll , ntoskip , ntoreturn , spec , fields ,
221+ def __init__ (self , flags , db , coll , ntoskip , spec , fields ,
222222 codec_options , read_preference , limit ,
223223 batch_size , read_concern ):
224224 self .flags = flags
225225 self .db = db
226226 self .coll = coll
227227 self .ntoskip = ntoskip
228- self .ntoreturn = ntoreturn
229228 self .spec = spec
230229 self .fields = fields
231230 self .codec_options = codec_options
@@ -260,12 +259,21 @@ def get_message(self, set_slave_ok, is_mongos, use_cmd=False):
260259
261260 ns = _UJOIN % (self .db , self .coll )
262261 spec = self .spec
263- ntoreturn = self .ntoreturn
264262
265263 if use_cmd :
266264 ns = _UJOIN % (self .db , "$cmd" )
267265 spec = self .as_command ()[0 ]
268266 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
269277
270278 if is_mongos :
271279 spec = _maybe_add_read_preference (spec ,
0 commit comments