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"));
}
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");
}
}
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");
}
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);
}
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");
}
Aggregations