Search in sources :

Example 16 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method shouldRetainNestedArrayPathWithPlaceholdersForMappedEntity.

// DATAMONGO-2502
@Test
void shouldRetainNestedArrayPathWithPlaceholdersForMappedEntity() {
    DefaultBulkOperations ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(OrderTest.class)), new QueryMapper(converter), new UpdateMapper(converter), null, null));
    ops.updateOne(new BasicQuery("{}"), Update.update("items.$.documents.0.fileId", "file-id")).execute();
    verify(collection).bulkWrite(captor.capture(), any());
    UpdateOneModel<Document> updateModel = (UpdateOneModel<Document>) captor.getValue().get(0);
    assertThat(updateModel.getUpdate()).isEqualTo(new Document("$set", new Document("items.$.documents.0.the_file_id", "file-id")));
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 17 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsIntegrationTests method createBulkOps.

private BulkOperations createBulkOps(BulkMode mode, Class<?> entityType) {
    Optional<? extends MongoPersistentEntity<?>> entity = entityType != null ? Optional.of(operations.getConverter().getMappingContext().getPersistentEntity(entityType)) : Optional.empty();
    BulkOperationContext bulkOperationContext = new BulkOperationContext(mode, entity, new QueryMapper(operations.getConverter()), new UpdateMapper(operations.getConverter()), null, null);
    DefaultBulkOperations bulkOps = new DefaultBulkOperations(operations, COLLECTION_NAME, bulkOperationContext);
    bulkOps.setDefaultWriteConcern(WriteConcern.ACKNOWLEDGED);
    return bulkOps;
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper)

Example 18 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultIndexOperationsIntegrationTests method shouldCreateIndexWithCollationCorrectly.

// DATAMONGO-1518
@Test
public void shouldCreateIndexWithCollationCorrectly() {
    IndexDefinition id = new Index().named("with-collation").on("xyz", Direction.ASC).collation(Collation.of("de_AT").caseFirst(CaseFirst.off()));
    new DefaultIndexOperations(template.getMongoDbFactory(), COLLECTION_NAME, new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    indexOps.ensureIndex(id);
    Document expected = // 
    new Document("locale", "de_AT").append("caseLevel", // 
    false).append("caseFirst", // 
    "off").append("strength", // 
    3).append("numericOrdering", // 
    false).append("alternate", // 
    "non-ignorable").append("maxVariable", // 
    "punct").append("normalization", // 
    false).append("backwards", false);
    IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "with-collation");
    assertThat(info.getCollation()).isPresent();
    // version is set by MongoDB server - we remove it to avoid errors when upgrading server version.
    Document result = info.getCollation().get();
    result.remove("version");
    assertThat(result).isEqualTo(expected);
}
Also used : IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 19 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultIndexOperationsIntegrationTests method shouldFavorExplicitMappingHintViaClass.

// DATAMONGO-1467, DATAMONGO-2198
@Test
public void shouldFavorExplicitMappingHintViaClass() {
    IndexDefinition id = new Index().named("partial-with-inheritance").on("k3y", Direction.ASC).partial(of(where("age").gte(10)));
    indexOps = new DefaultIndexOperations(template.getMongoDbFactory(), COLLECTION_NAME, new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    indexOps.ensureIndex(id);
    IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "partial-with-inheritance");
    assertThat(Document.parse(info.getPartialFilterExpression())).isEqualTo(Document.parse("{ \"a_g_e\" : { \"$gte\" : 10 } }"));
}
Also used : IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Test(org.junit.jupiter.api.Test)

Example 20 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class UnsetOperationUnitTests method contextFor.

private static AggregationOperationContext contextFor(@Nullable Class<?> type) {
    if (type == null) {
        return Aggregation.DEFAULT_CONTEXT;
    }
    MappingMongoConverter mongoConverter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, new MongoMappingContext());
    mongoConverter.afterPropertiesSet();
    return new TypeBasedAggregationOperationContext(type, mongoConverter.getMappingContext(), new QueryMapper(mongoConverter));
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper)

Aggregations

QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)29 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)19 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)19 Test (org.junit.jupiter.api.Test)14 Document (org.bson.Document)11 BulkOperationContext (org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext)7 UpdateMapper (org.springframework.data.mongodb.core.convert.UpdateMapper)7 BsonDocument (org.bson.BsonDocument)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)3 Index (org.springframework.data.mongodb.core.index.Index)3 IndexDefinition (org.springframework.data.mongodb.core.index.IndexDefinition)3 IndexInfo (org.springframework.data.mongodb.core.index.IndexInfo)3 BeforeConvertEvent (org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent)3 BeforeSaveEvent (org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)3 Test (org.junit.Test)2 MongoConverter (org.springframework.data.mongodb.core.convert.MongoConverter)2 AfterSaveEvent (org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent)2 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)2 MongoWriteException (com.mongodb.MongoWriteException)1