Skip to content

Commit 346349d

Browse files
committed
CDRIVER-735 aggregate uses cursor if wire version > 0
1 parent c9e7591 commit 346349d

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,28 @@ mongoc_collection_aggregate (mongoc_collection_t *collection, /* IN */
224224
const bson_t *options, /* IN */
225225
const mongoc_read_prefs_t *read_prefs) /* IN */
226226
{
227+
mongoc_server_description_t *selected_server;
228+
bson_error_t error;
227229
mongoc_cursor_t *cursor;
228230
bson_iter_t iter;
229231
bson_t command;
230232
bson_t child;
231233
int32_t batch_size = 0;
232-
bool try_cursor = true;
234+
bool use_cursor;
233235

234236
bson_return_val_if_fail (collection, NULL);
235237
bson_return_val_if_fail (pipeline, NULL);
236238

239+
selected_server = mongoc_topology_select(collection->client->topology,
240+
MONGOC_SS_READ,
241+
read_prefs,
242+
15,
243+
&error);
244+
245+
use_cursor = selected_server->max_wire_version >= 1;
246+
237247
bson_init (&command);
238248

239-
TOP:
240249
BSON_APPEND_UTF8 (&command, "aggregate", collection->collection);
241250

242251
/*
@@ -251,7 +260,7 @@ mongoc_collection_aggregate (mongoc_collection_t *collection, /* IN */
251260
}
252261

253262
/* for newer version, we include a cursor subdocument */
254-
if (try_cursor) {
263+
if (use_cursor) {
255264
bson_append_document_begin (&command, "cursor", 6, &child);
256265

257266
if (options && bson_iter_init (&iter, options)) {
@@ -280,18 +289,16 @@ mongoc_collection_aggregate (mongoc_collection_t *collection, /* IN */
280289
cursor = mongoc_collection_command (collection, flags, 0, 0, batch_size,
281290
&command, NULL, read_prefs);
282291

283-
if (try_cursor) {
292+
cursor->hint = selected_server->id;
293+
294+
if (use_cursor) {
284295
/* even for newer versions, we get back a cursor document, that we have
285296
* to patch in */
286-
287297
_mongoc_cursor_cursorid_init(cursor);
288298
cursor->limit = 0;
289299

290300
if (! _mongoc_cursor_cursorid_prime (cursor)) {
291301
mongoc_cursor_destroy (cursor);
292-
bson_reinit (&command);
293-
try_cursor = false;
294-
goto TOP;
295302
}
296303
} else {
297304
/* for older versions we get an array that we can create a synthetic

0 commit comments

Comments
 (0)