Search in sources :

Example 21 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-data-mongodb by spring-projects.

the class ExecutableUpdateOperationSupportTests method findAndModifyWithDifferentDomainTypeAndCollection.

// DATAMONGO-1563
@Test
void findAndModifyWithDifferentDomainTypeAndCollection() {
    Optional<Jedi> result = template.update(Jedi.class).inCollection(STAR_WARS).matching(query(where("_id").is(han.getId()))).apply(new Update().set("name", "Han")).findAndModify();
    assertThat(result.get()).hasFieldOrPropertyWithValue("name", "han");
    assertThat(template.findOne(queryHan(), Person.class)).isNotEqualTo(han).hasFieldOrPropertyWithValue("firstname", "Han");
}
Also used : Update(org.springframework.data.mongodb.core.query.Update) Test(org.junit.jupiter.api.Test)

Example 22 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-data-mongodb by spring-projects.

the class MongoTemplateDocumentReferenceTests method updateReferenceMapWithEntity.

// GH-3602
@Test
@Disabled("Property path resolution does not work inside maps, the key is considered :/")
void updateReferenceMapWithEntity() {
    String rootCollectionName = template.getCollectionName(CollectionRefRoot.class);
    CollectionRefRoot root = new CollectionRefRoot();
    root.id = "root-1";
    root.mapValueRef = Collections.singletonMap("beastie", new SimpleObjectRef("ref-1", "boys"));
    template.save(root);
    template.update(CollectionRefRoot.class).apply(new Update().set("mapValueRef.rise", new SimpleObjectRef("ref-2", "against"))).first();
    Document target = template.execute(db -> {
        return db.getCollection(rootCollectionName).find(Filters.eq("_id", "root-1")).first();
    });
    assertThat(target).containsEntry("mapValueRef", new Document("beastie", "ref-1").append("rise", "ref-2"));
}
Also used : ToString(lombok.ToString) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 23 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-data-mongodb by spring-projects.

the class MongoTemplateDocumentReferenceTests method updateReferenceHavingCustomizedIdTargetType.

// GH-3782
@Test
void updateReferenceHavingCustomizedIdTargetType() {
    ObjectId expectedIdValue = new ObjectId();
    String rootCollectionName = template.getCollectionName(SingleRefRoot.class);
    SingleRefRoot root = new SingleRefRoot();
    root.id = "root-1";
    template.save(root);
    template.update(SingleRefRoot.class).apply(new Update().set("customIdTargetRef", new ObjectRefHavingCustomizedIdTargetType(expectedIdValue.toString(), "b"))).first();
    Document target = template.execute(db -> {
        return db.getCollection(rootCollectionName).find(Filters.eq("_id", "root-1")).first();
    });
    assertThat(target).containsEntry("customIdTargetRef", expectedIdValue);
}
Also used : ObjectId(org.bson.types.ObjectId) ToString(lombok.ToString) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 24 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-data-mongodb by spring-projects.

the class MongoTemplateDocumentReferenceTests method updateReferenceMapWithValue.

// GH-3602
@Test
void updateReferenceMapWithValue() {
    String rootCollectionName = template.getCollectionName(CollectionRefRoot.class);
    CollectionRefRoot root = new CollectionRefRoot();
    root.id = "root-1";
    root.mapValueRef = Collections.singletonMap("beastie", new SimpleObjectRef("ref-1", "boys"));
    template.save(root);
    template.update(CollectionRefRoot.class).apply(new Update().set("mapValueRef.rise", "ref-2")).first();
    Document target = template.execute(db -> {
        return db.getCollection(rootCollectionName).find(Filters.eq("_id", "root-1")).first();
    });
    assertThat(target).containsEntry("mapValueRef", new Document("beastie", "ref-1").append("rise", "ref-2"));
}
Also used : ToString(lombok.ToString) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 25 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-data-mongodb by spring-projects.

the class MongoTemplateDocumentReferenceTests method updateReferenceWithEntityHavingPointerConversion.

// GH-3602
@Test
void updateReferenceWithEntityHavingPointerConversion() {
    WithRefB b = new WithRefB();
    b.id = "b";
    template.save(b);
    WithRefA a = new WithRefA();
    a.id = "a";
    template.save(a);
    template.update(WithRefA.class).apply(new Update().set("toB", b)).first();
    String collectionA = template.getCollectionName(WithRefA.class);
    Document target = template.execute(db -> {
        return db.getCollection(collectionA).find(Filters.eq("_id", "a")).first();
    });
    assertThat(target).containsEntry("toB", "b");
}
Also used : ToString(lombok.ToString) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Update (org.springframework.data.mongodb.core.query.Update)212 Test (org.junit.jupiter.api.Test)179 Document (org.bson.Document)117 Query (org.springframework.data.mongodb.core.query.Query)72 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)57 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)23 Bson (org.bson.conversions.Bson)21 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)15 UpdateResult (com.mongodb.client.result.UpdateResult)12 ObjectId (org.bson.types.ObjectId)12 List (java.util.List)8 ToString (lombok.ToString)8 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)8 Criteria (org.springframework.data.mongodb.core.query.Criteria)8 VersionedPerson (org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson)7 StepVerifier (reactor.test.StepVerifier)7 BsonDocument (org.bson.BsonDocument)6 UpdateOptions (com.mongodb.client.model.UpdateOptions)5 ArrayList (java.util.ArrayList)5 Instant (java.time.Instant)4