Spring Data MongoDB - Spring Web Services - Parent - SpringSource
Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource
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
- Page 18 and 19: please define productname in your d
- Page 20 and 21: please define productname in your d
- Page 22 and 23: please define productname in your d
- Page 24 and 25: please define productname in your d
- Page 26 and 27: please define productname in your d
- Page 28 and 29: please define productname in your d
- Page 30 and 31: please define productname in your d
- Page 32 and 33: please define productname in your d
- Page 34 and 35: please define productname in your d
- Page 36 and 37: please define productname in your d
- Page 38 and 39: please define productname in your d
- Page 40 and 41: please define productname in your d
- Page 42 and 43: please define productname in your d
- Page 44 and 45: please define productname in your d
- Page 46 and 47: please define productname in your d
- Page 48 and 49: please define productname in your d
- Page 50 and 51: please define productname in your d
- Page 52 and 53: please define productname in your d
- Page 54 and 55: please define productname in your d
- Page 56 and 57: please define productname in your d
- Page 58 and 59: please define productname in your d
- Page 60 and 61: please define productname in your d
- Page 62 and 63: please define productname in your d
- Page 64 and 65: please define productname in your d
- Page 66 and 67: please define productname in your d
- Page 70 and 71: please define productname in your d
- Page 72 and 73: please define productname in your d
- Page 74 and 75: please define productname in your d
- Page 76 and 77: please define productname in your d
- Page 78 and 79: please define productname in your d
- Page 80 and 81: please define productname in your d
- Page 82 and 83: please define productname in your d
- Page 84 and 85: please define productname in your d
- Page 86 and 87: please define productname in your d
- Page 88 and 89: please define productname in your d
- Page 90 and 91: please define productname in your d
- Page 92 and 93: please define productname in your d
- Page 94 and 95: please define productname in your d
- Page 96 and 97: please define productname in your d
- Page 98 and 99: please define productname in your d
- Page 100 and 101: please define productname in your d
- Page 102 and 103: please define productname in your d
- Page 104: please define productname in your d
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