Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 5c35d2b

Browse files
authored
Merge pull request #653 from miguelabautista/#652
#652: setting setHint to all queries
2 parents 2c80cb1 + 13c3092 commit 5c35d2b

File tree

1 file changed

+30
-9
lines changed
  • grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/query

1 file changed

+30
-9
lines changed

grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/query/MongoQuery.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.client.AggregateIterable;
2020
import com.mongodb.client.FindIterable;
2121
import com.mongodb.client.MongoCursor;
22+
import com.mongodb.client.MongoIterable;
2223
import grails.mongodb.geo.*;
2324
import groovy.lang.Closure;
2425
import org.bson.BsonDocument;
@@ -426,13 +427,15 @@ protected List executeQuery(final PersistentEntity entity, final Junction criter
426427
}
427428
final Object dbObject;
428429
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();
433435
} 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)
436439
.first();
437440
}
438441
if(dbObject == null) {
@@ -474,6 +477,7 @@ protected List executeQuery(final PersistentEntity entity, final Junction criter
474477

475478

476479
AggregateIterable<Document> aggregatedResults = collection.aggregate(aggregationPipeline);
480+
aggregatedResults = (AggregateIterable<Document>) setHint(aggregatedResults);
477481
final MongoCursor<Document> aggregateCursor = aggregatedResults.iterator();
478482

479483
if (singleResult && aggregateCursor.hasNext()) {
@@ -518,17 +522,34 @@ protected MongoCursor<Document> executeQuery(final PersistentEntity entity,
518522
cursor = executeQueryAndApplyPagination(collection, query);
519523
}
520524

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+
521533
if (queryArguments != null) {
522534
if (queryArguments.containsKey(HINT_ARGUMENT)) {
523535
Object hint = queryArguments.get(HINT_ARGUMENT);
524536
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+
}
526542
} 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+
}
528548
}
529549
}
530550
}
531-
return cursor.iterator();
551+
552+
return result;
532553
}
533554

534555
protected FindIterable<Document> executeQueryAndApplyPagination(com.mongodb.client.MongoCollection<Document> collection, Document query) {

0 commit comments

Comments
 (0)