DATAMONGO-2360 - Apply query hint to count queries.
Original pull request: #788.
This commit is contained in:
committed by
Mark Paluch
parent
dc859953f4
commit
313ffb5426
@@ -1130,6 +1130,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
CountOptions options = new CountOptions();
|
||||
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
|
||||
|
||||
if (StringUtils.hasText(query.getHint())) {
|
||||
options.hint(Document.parse(query.getHint()));
|
||||
}
|
||||
|
||||
Document document = queryMapper.getMappedObject(query.getQueryObject(),
|
||||
Optional.ofNullable(entityClass).map(it -> mappingContext.getPersistentEntity(entityClass)));
|
||||
|
||||
|
||||
@@ -910,6 +910,18 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
|
||||
is(equalTo(com.mongodb.client.model.Collation.builder().locale("fr").build())));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2360
|
||||
public void countShouldApplyQueryHintIfPresent() {
|
||||
|
||||
Document queryHint = new Document("age", 1);
|
||||
template.count(new BasicQuery("{}").withHint(queryHint), AutogenerateableId.class);
|
||||
|
||||
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
|
||||
verify(collection).count(any(), options.capture());
|
||||
|
||||
assertThat(options.getValue().getHint()).isEqualTo(queryHint);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1733
|
||||
public void appliesFieldsWhenInterfaceProjectionIsClosedAndQueryDoesNotDefineFields() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user