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