Search in sources :

Example 26 with Update

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

the class MongoTemplateDocumentReferenceTests method updateReferenceCollectionWithValue.

// GH-3602
@Test
void updateReferenceCollectionWithValue() {
    String rootCollectionName = template.getCollectionName(CollectionRefRoot.class);
    CollectionRefRoot root = new CollectionRefRoot();
    root.id = "root-1";
    root.simpleValueRef = Collections.singletonList(new SimpleObjectRef("ref-1", "beastie"));
    template.save(root);
    template.update(CollectionRefRoot.class).apply(new Update().push("simpleValueRef").value("ref-2")).first();
    Document target = template.execute(db -> {
        return db.getCollection(rootCollectionName).find(Filters.eq("_id", "root-1")).first();
    });
    assertThat(target).containsEntry("simpleValueRef", Arrays.asList("ref-1", "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 27 with Update

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

the class MongoTemplateTests method testUsingUpdateWithMultipleSet.

@Test
public void testUsingUpdateWithMultipleSet() throws Exception {
    template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class);
    PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
    p1.setFirstName("Sven");
    p1.setAge(11);
    template.insert(p1);
    PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
    p2.setFirstName("Mary");
    p2.setAge(21);
    template.insert(p2);
    Update u = new Update().set("firstName", "Bob").set("age", 10);
    UpdateResult wr = template.updateMulti(new Query(), u, PersonWithIdPropertyOfTypeObjectId.class);
    if (wr.wasAcknowledged()) {
        assertThat(wr.getModifiedCount()).isEqualTo(2L);
    }
    Query q1 = new Query(Criteria.where("age").in(11, 21));
    List<PersonWithIdPropertyOfTypeObjectId> r1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(r1.size()).isEqualTo(0);
    Query q2 = new Query(Criteria.where("age").is(10));
    List<PersonWithIdPropertyOfTypeObjectId> r2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(r2.size()).isEqualTo(2);
    for (PersonWithIdPropertyOfTypeObjectId p : r2) {
        assertThat(p.getAge()).isEqualTo(10);
        assertThat(p.getFirstName()).isEqualTo("Bob");
    }
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.jupiter.api.Test)

Example 28 with Update

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

the class MongoTemplateTests method findAndModifyShouldAddTypeInformationWithinUpdatedTypeOnEmbeddedDocumentWithCollectionWhenRewriting.

// DATAMONGO-1210
@Test
public void findAndModifyShouldAddTypeInformationWithinUpdatedTypeOnEmbeddedDocumentWithCollectionWhenRewriting() throws Exception {
    List<Model> models = Arrays.<Model>asList(new ModelA("value1"));
    DocumentWithEmbeddedDocumentWithCollection doc = new DocumentWithEmbeddedDocumentWithCollection(new DocumentWithCollection(models));
    template.save(doc);
    Query query = query(where("id").is(doc.id));
    Update update = Update.update("embeddedDocument", new DocumentWithCollection(Arrays.<Model>asList(new ModelA("value2"))));
    assertThat(template.findOne(query, DocumentWithEmbeddedDocumentWithCollection.class)).isNotNull();
    template.findAndModify(query, update, DocumentWithEmbeddedDocumentWithCollection.class);
    DocumentWithEmbeddedDocumentWithCollection retrieved = template.findOne(query, DocumentWithEmbeddedDocumentWithCollection.class);
    assertThat(retrieved).isNotNull();
    assertThat(retrieved.embeddedDocument.models).hasSize(1);
    assertThat(retrieved.embeddedDocument.models.get(0).value()).isEqualTo("value2");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) Test(org.junit.jupiter.api.Test)

Example 29 with Update

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

the class MongoTemplateTests method updateMultiShouldAddValuesCorrectlyWhenUsingAddToSetWithEach.

// DATAMONGO-471
@Test
public void updateMultiShouldAddValuesCorrectlyWhenUsingAddToSetWithEach() {
    DocumentWithCollectionOfSimpleType document = new DocumentWithCollectionOfSimpleType();
    document.values = Arrays.asList("spring");
    template.save(document);
    Query query = query(where("id").is(document.id));
    assertThat(template.findOne(query, DocumentWithCollectionOfSimpleType.class).values).hasSize(1);
    Update update = new Update().addToSet("values").each("data", "mongodb");
    template.updateMulti(query, update, DocumentWithCollectionOfSimpleType.class);
    assertThat(template.findOne(query, DocumentWithCollectionOfSimpleType.class).values).hasSize(3);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) Test(org.junit.jupiter.api.Test)

Example 30 with Update

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

the class MongoTemplateTests method findAndModifyAddToSetWithEachShouldNotAddDuplicatesNorTypeHintForSimpleDocuments.

// DATAMONGO-1210
@Test
public void findAndModifyAddToSetWithEachShouldNotAddDuplicatesNorTypeHintForSimpleDocuments() {
    DocumentWithCollectionOfSamples doc = new DocumentWithCollectionOfSamples();
    doc.samples = Arrays.asList(new Sample(null, "sample1"));
    template.save(doc);
    Query query = query(where("id").is(doc.id));
    assertThat(template.findOne(query, DocumentWithCollectionOfSamples.class)).isNotNull();
    Update update = new Update().addToSet("samples").each(new Sample(null, "sample2"), new Sample(null, "sample1"));
    template.findAndModify(query, update, DocumentWithCollectionOfSamples.class);
    DocumentWithCollectionOfSamples retrieved = template.findOne(query, DocumentWithCollectionOfSamples.class);
    assertThat(retrieved).isNotNull();
    assertThat(retrieved.samples).hasSize(2);
    assertThat(retrieved.samples.get(0).field).isEqualTo("sample1");
    assertThat(retrieved.samples.get(1).field).isEqualTo("sample2");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) 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