use of org.springframework.data.mongodb.core.query.Query in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method testUpdateShouldAllowMultiplePushAll.
// DATAMONGO-354, DATAMONGO-1824
@Test
@MongoVersion(until = "3.6")
@SuppressWarnings("deprecation")
public void testUpdateShouldAllowMultiplePushAll() {
DocumentWithMultipleCollections doc = new DocumentWithMultipleCollections();
doc.id = "1234";
doc.string1 = Arrays.asList("spring");
doc.string2 = Arrays.asList("one");
template.save(doc);
Update update = new Update().pushAll("string1", new Object[] { "data", "mongodb" });
update.pushAll("string2", new String[] { "two", "three" });
Query findQuery = new Query(Criteria.where("id").is(doc.id));
template.updateFirst(findQuery, update, DocumentWithMultipleCollections.class);
DocumentWithMultipleCollections result = template.findOne(findQuery, DocumentWithMultipleCollections.class);
assertThat(result.string1, hasItems("spring", "data", "mongodb"));
assertThat(result.string2, hasItems("one", "two", "three"));
}
use of org.springframework.data.mongodb.core.query.Query in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method updateMultiShouldIncreaseVersionOfAllUpdatedEntities.
// DATAMONGO-811
@Test
public void updateMultiShouldIncreaseVersionOfAllUpdatedEntities() {
VersionedPerson person1 = new VersionedPerson();
person1.firstname = "Dave";
VersionedPerson person2 = new VersionedPerson();
person2.firstname = "Dave";
template.save(person1);
template.save(person2);
Query q = query(where("id").in(person1.id, person2.id));
template.updateMulti(q, Update.update("lastname", "Metthews"), VersionedPerson.class);
for (VersionedPerson p : template.find(q, VersionedPerson.class)) {
assertThat(p.version, equalTo(Long.valueOf(1)));
}
}
use of org.springframework.data.mongodb.core.query.Query in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method updateFirstShouldDoNothingWhenCalledForEntitiesThatDoNotExist.
// DATAMONOGO-828
@Test
public void updateFirstShouldDoNothingWhenCalledForEntitiesThatDoNotExist() {
Query q = query(where("id").is(Long.MIN_VALUE));
template.updateFirst(q, Update.update("lastname", "supercalifragilisticexpialidocious"), VersionedPerson.class);
assertThat(template.findOne(q, VersionedPerson.class), nullValue());
}
use of org.springframework.data.mongodb.core.query.Query in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method testUsingAnInQueryWithStringId.
@Test
public void testUsingAnInQueryWithStringId() throws Exception {
template.remove(new Query(), PersonWithIdPropertyOfTypeString.class);
PersonWithIdPropertyOfTypeString p1 = new PersonWithIdPropertyOfTypeString();
p1.setFirstName("Sven");
p1.setAge(11);
template.insert(p1);
PersonWithIdPropertyOfTypeString p2 = new PersonWithIdPropertyOfTypeString();
p2.setFirstName("Mary");
p2.setAge(21);
template.insert(p2);
PersonWithIdPropertyOfTypeString p3 = new PersonWithIdPropertyOfTypeString();
p3.setFirstName("Ann");
p3.setAge(31);
template.insert(p3);
PersonWithIdPropertyOfTypeString p4 = new PersonWithIdPropertyOfTypeString();
p4.setFirstName("John");
p4.setAge(41);
template.insert(p4);
Query q1 = new Query(Criteria.where("age").in(11, 21, 41));
List<PersonWithIdPropertyOfTypeString> results1 = template.find(q1, PersonWithIdPropertyOfTypeString.class);
Query q2 = new Query(Criteria.where("firstName").in("Ann", "Mary"));
List<PersonWithIdPropertyOfTypeString> results2 = template.find(q2, PersonWithIdPropertyOfTypeString.class);
Query q3 = new Query(Criteria.where("id").in(p3.getId(), p4.getId()));
List<PersonWithIdPropertyOfTypeString> results3 = template.find(q3, PersonWithIdPropertyOfTypeString.class);
assertThat(results1.size(), is(3));
assertThat(results2.size(), is(2));
assertThat(results3.size(), is(2));
}
use of org.springframework.data.mongodb.core.query.Query in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method aQueryWithNoRestrictedResultTypesShouldReturnAllInstancesWithinTheGivenCollection.
// DATAMONGO-709
@Test
public void aQueryWithNoRestrictedResultTypesShouldReturnAllInstancesWithinTheGivenCollection() {
BaseDoc doc0 = new BaseDoc();
doc0.value = "foo";
SpecialDoc doc1 = new SpecialDoc();
doc1.value = "foo";
doc1.specialValue = "specialfoo";
VerySpecialDoc doc2 = new VerySpecialDoc();
doc2.value = "foo";
doc2.specialValue = "specialfoo";
doc2.verySpecialValue = 4711;
String collectionName = template.getCollectionName(BaseDoc.class);
template.insert(doc0, collectionName);
template.insert(doc1, collectionName);
template.insert(doc2, collectionName);
Query query = Query.query(where("value").is("foo"));
List<BaseDoc> result = template.find(query, BaseDoc.class);
assertThat(result, is(notNullValue()));
assertThat(result.size(), is(3));
assertThat(result.get(0).getClass(), is((Object) BaseDoc.class));
assertThat(result.get(1).getClass(), is((Object) SpecialDoc.class));
assertThat(result.get(2).getClass(), is((Object) VerySpecialDoc.class));
}
Aggregations