Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource

static.springsource.org
from static.springsource.org More from this publisher
13.07.2015 Views

please define productname in your docbook file!Converter disambiguationGenerally we inspect the Converter implementations for the source and target types they convert fromand to. Depending on whether one of those is a type MongoDB can handle natively we will register theconverter instance as reading or writing one. Have a look at the following samples:// Write converter as only the target type is one Mongo can handle nativelyclass MyConverter implements Converter { … }// Read converter as only the source type is one Mongo can handle nativelyclass MyConverter implements Converter { … }In case you write a Converter whose source and target type are native Mongo types there's noway for us to determine whether we should consider it as reading or writing converter. Registering theconverter instance as both might lead to unwanted results then. E.g. a Converteris ambiguous although it probably does not make sense to try to convert all Strings into Longs whenwriting. To be generally able to force the infrastructure to register a converter for one way only we provide@ReadingConverter as well as @WritingConverter to be used at the converter implementation.5.11 Index and Collection managmentMongoTemplate provides a few methods for managing indexes and collections. These are collectedinto a helper interface called IndexOperations. You access these operations by calilng the methodindexOps and pass in either the collection name or the java.lang.Class of your entity (the collectionname will be derived from the .class either by name or via annotation metadata).The IndexOperations interface is shown belowpublic interface IndexOperations {void ensureIndex(IndexDefinition indexDefinition);void dropIndex(String name);void dropAllIndexes();void resetIndexCache();}List getIndexInfo();Methods for creating an IndexWe can create an index on a collection to improve query performance.mongoTemplate.indexOps(Person.class).ensureIndex(new Index().on("name",Order.ASCENDING));Example 5.23 Creating an index using the MongoTemplate• ensureIndex Ensure that an index for the provided IndexDefinition exists for the collection.1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 63

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 MongoDB driver's DB.command( ) method using the executeCommand(…)methods on MongoTemplate. These will also perform exception translation into Spring'sDataAccessException hierarchy.Methods for executing commands• CommandResult executeCommand (DBObject command) Execute a MongoDB command.1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 64

please define productname in your docbook file!Converter disambiguationGenerally we inspect the Converter implementations for the source and target types they convert fromand to. Depending on whether one of those is a type <strong>MongoDB</strong> can handle natively we will register theconverter instance as reading or writing one. Have a look at the following samples:// Write converter as only the target type is one Mongo can handle nativelyclass MyConverter implements Converter { … }// Read converter as only the source type is one Mongo can handle nativelyclass MyConverter implements Converter { … }In case you write a Converter whose source and target type are native Mongo types there's noway for us to determine whether we should consider it as reading or writing converter. Registering theconverter instance as both might lead to unwanted results then. E.g. a Converteris ambiguous although it probably does not make sense to try to convert all Strings into Longs whenwriting. To be generally able to force the infrastructure to register a converter for one way only we provide@ReadingConverter as well as @WritingConverter to be used at the converter implementation.5.11 Index and Collection managmentMongoTemplate provides a few methods for managing indexes and collections. These are collectedinto a helper interface called IndexOperations. You access these operations by calilng the methodindexOps and pass in either the collection name or the java.lang.Class of your entity (the collectionname will be derived from the .class either by name or via annotation metadata).The IndexOperations interface is shown belowpublic interface IndexOperations {void ensureIndex(IndexDefinition indexDefinition);void dropIndex(String name);void dropAllIndexes();void resetIndexCache();}List getIndexInfo();Methods for creating an IndexWe can create an index on a collection to improve query performance.mongoTemplate.indexOps(Person.class).ensureIndex(new Index().on("name",Order.ASCENDING));Example 5.23 Creating an index using the MongoTemplate• ensureIndex Ensure that an index for the provided IndexDefinition exists for the collection.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 63

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

Saved successfully!

Ooh no, something went wrong!