|
19 | 19 | import com.mongodb.client.AggregateIterable;
|
20 | 20 | import com.mongodb.client.FindIterable;
|
21 | 21 | import com.mongodb.client.MongoCursor;
|
| 22 | +import com.mongodb.client.MongoIterable; |
22 | 23 | import grails.mongodb.geo.*;
|
23 | 24 | import groovy.lang.Closure;
|
24 | 25 | import org.bson.BsonDocument;
|
@@ -426,13 +427,15 @@ protected List executeQuery(final PersistentEntity entity, final Junction criter
|
426 | 427 | }
|
427 | 428 | final Object dbObject;
|
428 | 429 | if (criteria.isEmpty()) {
|
429 |
| - dbObject = collection |
430 |
| - .find(createQueryObject(entity)) |
431 |
| - .limit(1) |
432 |
| - .first(); |
| 430 | + FindIterable<Document> cursor = collection |
| 431 | + .find(createQueryObject(entity)); |
| 432 | + |
| 433 | + dbObject = ((FindIterable<Document>) setHint(cursor)).limit(1) |
| 434 | + .first(); |
433 | 435 | } else {
|
434 |
| - dbObject = collection.find(getMongoQuery()) |
435 |
| - .limit(1) |
| 436 | + FindIterable<Document> cursor = collection.find(getMongoQuery()); |
| 437 | + |
| 438 | + dbObject = ((FindIterable<Document>) setHint(cursor)).limit(1) |
436 | 439 | .first();
|
437 | 440 | }
|
438 | 441 | if(dbObject == null) {
|
@@ -474,6 +477,7 @@ protected List executeQuery(final PersistentEntity entity, final Junction criter
|
474 | 477 |
|
475 | 478 |
|
476 | 479 | AggregateIterable<Document> aggregatedResults = collection.aggregate(aggregationPipeline);
|
| 480 | + aggregatedResults = (AggregateIterable<Document>) setHint(aggregatedResults); |
477 | 481 | final MongoCursor<Document> aggregateCursor = aggregatedResults.iterator();
|
478 | 482 |
|
479 | 483 | if (singleResult && aggregateCursor.hasNext()) {
|
@@ -518,17 +522,34 @@ protected MongoCursor<Document> executeQuery(final PersistentEntity entity,
|
518 | 522 | cursor = executeQueryAndApplyPagination(collection, query);
|
519 | 523 | }
|
520 | 524 |
|
| 525 | + cursor = (FindIterable<Document>) setHint(cursor); |
| 526 | + |
| 527 | + return cursor.iterator(); |
| 528 | + } |
| 529 | + |
| 530 | + private MongoIterable<Document> setHint(MongoIterable<Document> cursor) { |
| 531 | + MongoIterable<Document> result = cursor; |
| 532 | + |
521 | 533 | if (queryArguments != null) {
|
522 | 534 | if (queryArguments.containsKey(HINT_ARGUMENT)) {
|
523 | 535 | Object hint = queryArguments.get(HINT_ARGUMENT);
|
524 | 536 | if (hint instanceof Map) {
|
525 |
| - cursor = cursor.hint(new Document((Map<String, Object>) hint)); |
| 537 | + if (cursor instanceof FindIterable) { |
| 538 | + result = ((FindIterable) cursor).hint(new Document((Map<String, Object>) hint)); |
| 539 | + } else if (cursor instanceof AggregateIterable) { |
| 540 | + result = ((AggregateIterable) cursor).hint(new Document((Map<String, Object>) hint)); |
| 541 | + } |
526 | 542 | } else {
|
527 |
| - cursor = cursor.hintString(hint.toString()); |
| 543 | + if (cursor instanceof FindIterable) { |
| 544 | + result = ((FindIterable) cursor).hintString(hint.toString()); |
| 545 | + } else if (cursor instanceof AggregateIterable) { |
| 546 | + result = ((AggregateIterable) cursor).hintString(hint.toString()); |
| 547 | + } |
528 | 548 | }
|
529 | 549 | }
|
530 | 550 | }
|
531 |
| - return cursor.iterator(); |
| 551 | + |
| 552 | + return result; |
532 | 553 | }
|
533 | 554 |
|
534 | 555 | protected FindIterable<Document> executeQueryAndApplyPagination(com.mongodb.client.MongoCollection<Document> collection, Document query) {
|
|
0 commit comments