Search in sources :

Example 41 with Query

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"));
}
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.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 42 with Query

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)));
    }
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.Test)

Example 43 with Query

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());
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.Test)

Example 44 with Query

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));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.Test)

Example 45 with Query

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));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.Test)

Aggregations

Query (org.springframework.data.mongodb.core.query.Query)488 Test (org.junit.Test)312 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)156 Update (org.springframework.data.mongodb.core.query.Update)79 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)66 Document (org.bson.Document)56 Criteria (org.springframework.data.mongodb.core.query.Criteria)47 PartTree (org.springframework.data.repository.query.parser.PartTree)44 Sort (org.springframework.data.domain.Sort)30 ObjectId (org.bson.types.ObjectId)27 BasicDBObject (com.mongodb.BasicDBObject)22 DBObject (com.mongodb.DBObject)20 Before (org.junit.Before)18 List (java.util.List)17 Autowired (org.springframework.beans.factory.annotation.Autowired)17 GeoJsonPoint (org.springframework.data.mongodb.core.geo.GeoJsonPoint)17 Collectors (java.util.stream.Collectors)16 ArrayList (java.util.ArrayList)15 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)15 MappingException (org.springframework.data.mapping.MappingException)15