Package org.spring.mongodb.example

Examples of org.spring.mongodb.example.Person


    // MongoOperations mongoOps = mongoTemplate;
    // mongoTemplate.dropCollection(Person.class);

    mongoTemplate.dropCollection("person");

    Person p = new Person("lichhao", 23);
    // Insert is used to initially store the object into the database.
    mongoTemplate.insert(p);

    log.info("Insert: " + p); // Find

    p = mongoTemplate.findById(p.getId(), Person.class);

    log.info("Found: " + p); // Update

    assertEquals(23, p.getAge());

    mongoTemplate.updateFirst(query(where("name").is("lichhao")),
        update("age", 35), Person.class);

    p = mongoTemplate.findOne(query(where("name").is("lichhao")),
        Person.class);

    log.info("Updated: " + p); // Delete mongoTemplate.remove(p);

    assertEquals(35, p.getAge());

    // Check that deletion worked
    List<Person> people = mongoTemplate.findAll(Person.class);

    log.info("Number of people = : " + people.size());

    assertEquals(1, people.size());

    // --------------------------------------

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

    Query queryObj = new Query(Criteria.where("name").is("Harry"));

    Update updateObj = new Update().inc("age", 1);

    // return's old person object
    p = mongoTemplate.findAndModify(queryObj, updateObj, Person.class);

    assertThat(p.getName(), is("Harry"));
    assertThat(p.getAge(), is(23));

    p = mongoTemplate.findOne(queryObj, Person.class);
    assertThat(p.getAge(), is(24));

    // Now return the newly updated document when updating
    p = mongoTemplate.findAndModify(queryObj, updateObj,
        new FindAndModifyOptions().returnNew(true), Person.class);
    assertThat(p.getAge(), is(25));
    // -------------------------------------

    Query queryObj2 = new Query(Criteria.where("name").is("Mary"));
    p = mongoTemplate.findAndModify(
        queryObj2,
        updateObj,
        new FindAndModifyOptions().returnNew(true).upsert(true)
            .remove(false), Person.class);

    assertThat(p.getName(), is("Mary"));
    assertThat(p.getAge(), is(1));

    // mongoTemplate.dropCollection(Person.class);
    mongoTemplate.dropCollection("person");
  }
View Full Code Here

TOP

Related Classes of org.spring.mongodb.example.Person

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.