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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

please define productname in your docbook file!You can create both standard indexes and geospatial indexes using the classes IndexDefinitionand GeoSpatialIndex respectfully. For example, given the Venue class defined in a previous section,you would declare a geospatial query as shown belowmongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location"));Accessing index informationThe IndexOperations interface has the method getIndexInfo that returns a list of IndexInfo objects. Thiscontains all the indexes defined on the collectcion. Here is an example that defines an index on thePerson class that has age property.template.indexOps(Person.class).ensureIndex(new Index().on("age",Order.DESCENDING).unique(Duplicates.DROP));List indexInfoList = template.indexOps(Person.class).getIndexInfo();// Contains// [IndexInfo [fieldSpec={_id=ASCENDING}, name=_id_, unique=false, dropDuplicates=false,sparse=false],// IndexInfo [fieldSpec={age=DESCENDING}, name=age_-1, unique=true, dropDuplicates=true,sparse=false]]Methods for working with a CollectionIt's time to look at some code examples showing how to use the MongoTemplate. First we look atcreating our first collection.DBCollection collection = null;if (!mongoTemplate.getCollectionNames().contains("MyNewCollection")) {collection = mongoTemplate.createCollection("MyNewCollection");}mongoTemplate.dropCollection("MyNewCollection");Example 5.24 Working with collections using the MongoTemplate• getCollectionNames Returns a set of collection names.• collectionExists Check to see if a collection with a given name exists.• createCollection Create an uncapped collection• dropCollection Drop the collection• getCollection Get a collection by name, creating it if it doesn't exist.5.12 Executing CommandsYou can also get at the <strong>MongoDB</strong> driver's DB.command( ) method using the executeCommand(…)methods on MongoTemplate. These will also perform exception translation into <strong>Spring</strong>'s<strong>Data</strong>AccessException hierarchy.Methods for executing commands• CommandResult executeCommand (DBObject command) Execute a <strong>MongoDB</strong> command.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 64

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

Saved successfully!

Ooh no, something went wrong!