13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

please define productname in your docbook file!package org.spring.example;import static org.springframework.data.mongodb.core.query.Criteria.where;import static org.springframework.data.mongodb.core.query.Update.update;import static org.springframework.data.mongodb.core.query.Query.query;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.data.mongodb.core.MongoOperations;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.SimpleMongoDbFactory;import com.mongodb.Mongo;public class MongoApp {private static final Log log = LogFactory.getLog(MongoApp.class);public static void main(String[] args) throws Exception {MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(newMongo(), "database"));Person p = new Person("Joe", 34);// Insert is used to initially store the object into the database.mongoOps.insert(p);log.info("Insert: " + p);// Findp = mongoOps.findById(p.getId(), Person.class);log.info("Found: " + p);// UpdatemongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class);p = mongoOps.findOne(query(where("name").is("Joe")), Person.class);log.info("Updated: " + p);// DeletemongoOps.remove(p);// Check that deletion workedList people = mongoOps.findAll(Person.class);log.info("Number of people = : " + people.size());}}mongoOps.dropCollection(Person.class);This would produce the following log output (including debug messages from MongoTemplate itself)1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!