Package org.springframework.data.mongodb.core.query

Examples of org.springframework.data.mongodb.core.query.Update


    template.insert(new Person("Tom", 21));
    template.insert(new Person("Dick", 22));
    template.insert(new Person("Harry", 23));

    Query query = new Query(Criteria.where("firstName").is("Harry"));
    Update update = new Update().inc("age", 1);
    Person p = template.findAndModify(query, update, Person.class); // return old
    assertThat(p.getFirstName(), is("Harry"));
    assertThat(p.getAge(), is(23));
    p = template.findOne(query, Person.class);
    assertThat(p.getAge(), is(24));
View Full Code Here


  @Test
  public void testFindAndUpdateUpsert() {
    template.insert(new Person("Tom", 21));
    template.insert(new Person("Dick", 22));
    Query query = new Query(Criteria.where("firstName").is("Harry"));
    Update update = new Update().set("age", 23);
    Person p = template.findAndModify(query, update, new FindAndModifyOptions().upsert(true).returnNew(true),
        Person.class);
    assertThat(p.getFirstName(), is("Harry"));
    assertThat(p.getAge(), is(23));
  }
View Full Code Here

    PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
    p2.setFirstName("Mary");
    p2.setAge(21);
    template.insert(p2);

    Update u = new Update().set("firstName", "Bob").set("age", 10);

    WriteResult wr = template.updateMulti(new Query(), u, PersonWithIdPropertyOfTypeObjectId.class);

    assertThat(wr.getN(), is(2));
View Full Code Here

    assertThat(lastWriteConcern, equalTo(WriteConcern.NONE));

    FsyncSafeWriteConcernResolver resolver = new FsyncSafeWriteConcernResolver();
    template.setWriteConcernResolver(resolver);
    Query q = query(where("_id").is(person.getId()));
    Update u = update("firstName", "Carter");
    result = template.updateFirst(q, u, PersonWithIdPropertyOfTypeObjectId.class);
    lastWriteConcern = result.getLastConcern();
    assertThat(lastWriteConcern, equalTo(WriteConcern.FSYNC_SAFE));

    MongoAction lastMongoAction = resolver.getMongoAction();
View Full Code Here

    entity.emailAddress = "old";

    template.save(entity);

    Query query = query(where("_id").is(entity.id));
    Update update = Update.update("emailAddress", "new");

    FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true);
    TypeWithFieldAnnotation result = template.findAndModify(query, update, options, TypeWithFieldAnnotation.class);
    assertThat(result.emailAddress, is("new"));
  }
View Full Code Here

    String idValue = "4711";
    Query query = new Query(Criteria.where("id").is(idValue));

    String fieldValue = "bubu";
    Update update = Update.update("field", fieldValue);

    template.upsert(query, update, Sample.class);
    Sample result = template.findOne(query, Sample.class);

    assertThat(result, is(notNullValue()));
View Full Code Here

    doc.model = new ModelA("foo");
    template.insert(doc);

    Query query = new Query(Criteria.where("id").is(doc.id));
    String newModelValue = "bar";
    Update update = Update.update("model", new ModelA(newModelValue));
    template.updateFirst(query, update, Document.class);

    Document result = template.findOne(query, Document.class);

    assertThat(result, is(notNullValue()));
View Full Code Here

    document.model = new ModelA("value1");

    template.save(document);

    Query query = query(where("id").is(document.id));
    Update update = Update.update("model", new ModelA("value2"));
    template.findAndModify(query, update, Document.class);

    Document retrieved = template.findOne(query, Document.class);
    assertThat(retrieved.model, instanceOf(ModelA.class));
    assertThat(retrieved.model.value(), equalTo("value2"));
View Full Code Here

    template.insert(doc);

    Query query = new Query(Criteria.where("id").is(doc.id));
    query.addCriteria(where("models.value").is("foo"));
    String newModelValue = "bar";
    Update update = Update.update("models.$", new ModelA(newModelValue));
    template.updateFirst(query, update, DocumentWithCollection.class);

    Query findQuery = new Query(Criteria.where("id").is(doc.id));
    DocumentWithCollection result = template.findOne(findQuery, DocumentWithCollection.class);
View Full Code Here

    DocumentWithCollection document = new DocumentWithCollection(Collections.<Model> emptyList());
    template.save(document);
    Query query = query(where("id").is(document.id));
    assumeThat(template.findOne(query, DocumentWithCollection.class).models, hasSize(1));

    Update update = new Update().push("models").each(new ModelA("model-b"), new ModelA("model-c"));
    template.updateMulti(query, update, DocumentWithCollection.class);

    assertThat(template.findOne(query, DocumentWithCollection.class).models, hasSize(3));
  }
View Full Code Here

TOP

Related Classes of org.springframework.data.mongodb.core.query.Update

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.