@@ -106,13 +106,17 @@ def __send_message(self, operation):
106106
107107 cmd_duration = response .duration
108108 rqst_id = response .request_id
109+ from_command = response .from_command
110+
109111 if publish :
110112 start = datetime .datetime .now ()
111113 try :
112114 doc = helpers ._unpack_response (response .data ,
113115 self .__id ,
114116 self .__collection .codec_options )
115- # TODO: check_command_response when getMore works with agg cursor
117+ if from_command :
118+ helpers ._check_command_response (doc ['data' ][0 ])
119+
116120 except OperationFailure as exc :
117121 self .__killed = True
118122
@@ -142,22 +146,30 @@ def __send_message(self, operation):
142146 self .__address )
143147 raise
144148
149+ if from_command :
150+ cursor = doc ['data' ][0 ]['cursor' ]
151+ documents = cursor ['nextBatch' ]
152+ self .__id = cursor ['id' ]
153+ self .__retrieved += len (documents )
154+ else :
155+ documents = doc ["data" ]
156+ self .__id = doc ["cursor_id" ]
157+ self .__retrieved += doc ["number_returned" ]
158+
145159 if publish :
146160 duration = (datetime .datetime .now () - start ) + cmd_duration
147161 # Must publish in getMore command response format.
148- res = {"cursor" : {"id" : doc [ "cursor_id" ] ,
162+ res = {"cursor" : {"id" : self . __id ,
149163 "ns" : self .__collection .full_name ,
150- "nextBatch" : doc [ "data" ] },
164+ "nextBatch" : documents },
151165 "ok" : 1 }
152166 listeners .publish_command_success (
153167 duration , res , "getMore" , rqst_id , self .__address )
154168
155- self .__id = doc ["cursor_id" ]
156169 if self .__id == 0 :
157170 self .__killed = True
171+ self .__data = deque (documents )
158172
159- self .__retrieved += doc ["number_returned" ]
160- self .__data = deque (doc ["data" ])
161173
162174 def _refresh (self ):
163175 """Refreshes the cursor with more data from the server.
@@ -175,8 +187,7 @@ def _refresh(self):
175187 self .__collection .name ,
176188 self .__batch_size ,
177189 self .__id ,
178- self .__collection .codec_options ,
179- cmd_cursor = True ))
190+ self .__collection .codec_options ))
180191
181192 else : # Cursor id is zero nothing else to return
182193 self .__killed = True
0 commit comments