Package org.springframework.data.mongodb.core

Examples of org.springframework.data.mongodb.core.MongoOperations


  private static final Log log = LogFactory.getLog(MongoApp.class);

  public static void main(String[] args) throws Exception {
  BasicConfigurator.configure();   
   
    MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(new Mongo(), "database"));

    // Insert is used to initially store the object into the database.
    Car c = new Car("Red", "Opel");
    mongoOps.insert(c);
    Person p = new Person("Joe", 34, c);
    Person p2 = new Person("Anna", 31, c);
    mongoOps.insert(p);
    mongoOps.insert(p2);
    log.info("Insert: " + p);
   
    // Find
    p = mongoOps.findById(p.getId(), Person.class);   
    log.info("Found: " + p);
   
    // Update
    mongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class);
    //mongoOps.updateMulti(query(where("name").is("Joe")), update("age", 36), Person.class);
    p = mongoOps.findOne(query(where("name").is("Joe")), Person.class);
    log.info("Updated: " + p);
   
    // Delete
    mongoOps.remove(p);
   
    // Check that deletion worked
    List<Person> people =  mongoOps.findAll(Person.class);
    log.info("Number of people = : " + people.size());

   
    //mongoOps.dropCollection(Person.class);
  }
View Full Code Here

TOP

Related Classes of org.springframework.data.mongodb.core.MongoOperations

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.