diff --git a/src/docbkx/reference/mapping.xml b/src/docbkx/reference/mapping.xml index 766ce17b6..8e59ae52d 100644 --- a/src/docbkx/reference/mapping.xml +++ b/src/docbkx/reference/mapping.xml @@ -69,13 +69,14 @@
How the '_id' field is handled in the mapping layer - Mongo requires that you have an '_id' field for all documents. If - you don't provide one the driver will assign a ObjectId with a generated - value. The "_id" field can be of any type the, other than arrays, so - long as it is unique. The driver naturally supports all primitive types - and Dates. When using the MongoMappingConverter - there are certain rules that govern how properties from the Java class - is mapped to this '_id' field. + MongoDB requires that you have an '_id' field for all documents. + If you don't provide one the driver will assign a ObjectId with a + generated value. The "_id" field can be of any type the, other than + arrays, so long as it is unique. The driver naturally supports all + primitive types and Dates. When using the + MongoMappingConverter there are certain rules + that govern how properties from the Java class is mapped to this '_id' + field. The following outlines what field will be mapped to the '_id' document field: @@ -212,7 +213,7 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration { getUserCredentials() to provide the username and password information to connect to the database. - Spring's Mongo namespace enables you to easily enable mapping + Spring's MongoDB namespace enables you to easily enable mapping functionality in XML @@ -381,7 +382,7 @@ public class Person { The mapping metadata infrastructure is defined in a seperate spring-data-commons project that is technology agnostic. Specific - subclasses are using in the Mongo support to support annotation based + subclasses are using in the MongoDB support to support annotation based metadata. Other strategies are also possible to put in place if there is demand. diff --git a/src/docbkx/reference/mongo-repositories.xml b/src/docbkx/reference/mongo-repositories.xml index 2102098f4..1b63405a6 100644 --- a/src/docbkx/reference/mongo-repositories.xml +++ b/src/docbkx/reference/mongo-repositories.xml @@ -2,7 +2,7 @@ - Mongo repositories + MongoDB repositories
Introduction @@ -60,7 +60,7 @@ add - General mongo repository Spring configuration + General MongoDB repository Spring configuration <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" @@ -129,7 +129,7 @@ public class PersonRepositoryTests { Query methods Most of the data access operations you usually trigger on a - repository result a query being executed against the Mongo databases. + repository result a query being executed against the MongoDB databases. Defining such a query is just a matter of declaring a method on the repository interface @@ -346,11 +346,11 @@ Distance distance = new Distance(200, Metrics.KILOMETERS);
- Mongo JSON based query methods and field restriction + MongoDB JSON based query methods and field restriction By adding the annotation org.springframework.data.mongodb.repository.Query - repository finder methods you can specify a Mongo JSON query string to + repository finder methods you can specify a MongoDB JSON query string to use instead of having the query derived from the method name. For example @@ -382,7 +382,7 @@ Distance distance = new Distance(200, Metrics.KILOMETERS);
Type-safe Query methods - Mongo repository support integrates with the MongoDB repository support integrates with the QueryDSL project which provides a means to perform type-safe queries in Java. To quote from the project description, "Instead of writing queries as inline strings or @@ -469,4 +469,4 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"), MongoDB queries.
-
\ No newline at end of file + diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index 4a8a871f5..1dccb1d60 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -88,9 +88,10 @@ First you need to set up a running Mongodb server. Refer to the Mongodb Quick - Start guide for an explanation on how to startup a Mongo instance. - Once installed starting Mongo is typically a matter of executing the - following command: MONGO_HOME/bin/mongod + Start guide for an explanation on how to startup a MongoDB + instance. Once installed starting MongoDB is typically a matter of + executing the following command: + MONGO_HOME/bin/mongod
To create a Spring project in STS go to File -> New -> Spring Template Project -> Simple Spring Utility Project --> press Yes when @@ -246,7 +247,7 @@ public class MongoApp { Required Jars - The following jars are required to use Spring Data Mongo + The following jars are required to use Spring Data MongoDB @@ -415,7 +416,7 @@ public class AppConfig { compared to instantiating a com.mongodb.Mongo instance directly, the FactoryBean approach does not throw a checked exception and has the added advantage of also providing the container - with an ExceptionTranslator implementation that translates Mongo + with an ExceptionTranslator implementation that translates MongoDB exceptions to exceptions in Spring's portable DataAccessException hierarchy for data access classes annoated with the @Repository annotation. @@ -529,11 +530,11 @@ public class AppConfig {
The MongoDbFactory interface - While com.mongodb.Mongo is the entry point to the MongoDB driver - API, connecting to a specific MongoDB database instance requires - additional information such as the database name and an optional - username and password. With that information you can obtain a - com.mongodb.DB object and access all the functionality of a specific + While com.mongodb.Mongo is the entry point + to the MongoDB driver API, connecting to a specific MongoDB database + instance requires additional information such as the database name and + an optional username and password. With that information you can obtain + a com.mongodb.DB object and access all the functionality of a specific MongoDB database instance. Spring provides the org.springframework.data.mongodb.core.MongoDbFactory interface shown below to bootstrap connectivity to the database. @@ -644,8 +645,8 @@ public class MongoConfiguration { the id attribute is specified. You can also provide the host and port for the underlying - com.mongodb.Mongo instance as shown below, in addition to username and - password for the database. + com.mongodb.Mongo instance as shown below, in + addition to username and password for the database. <mongo:db-factory id="anotherMongoDbFactory" host="localhost" @@ -705,7 +706,7 @@ public class MongoConfiguration { thread-safe and can be reused across multiple instances. - The mapping between Mongo documents and domain classes is done by + The mapping between MongoDB documents and domain classes is done by delegating to an implementation of the interface MongoConverter. Spring provides two implementations, SimpleMappingConverter and @@ -750,17 +751,17 @@ public class MongoConfiguration { Another central feature of MongoTemplate is exception translation of - exceptions thrown in the Mongo Java driver into Spring's portable Data + exceptions thrown in the MongoDB Java driver into Spring's portable Data Access Exception hierarchy. Refer to the section on exception translation for more information. While there are many convenience methods on MongoTemplate to help you easily perform common - tasks if you should need to access the Mongo driver API directly to access - functionality not explicitly exposed by the MongoTemplate you can use one - of several Execute callback methods to access underlying driver APIs. The - execute callbacks will give you a reference to either a + tasks if you should need to access the MongoDB driver API directly to + access functionality not explicitly exposed by the MongoTemplate you can + use one of several Execute callback methods to access underlying driver + APIs. The execute callbacks will give you a reference to either a com.mongodb.Collection or a com.mongodb.DB object. Please see the section Execution Callbacks for more @@ -801,8 +802,8 @@ public class AppConfig { MongoTemplate (Mongo mongo, String databaseName) - takes the - com.mongodb.Mongo object and the default database name to operate - against. + com.mongodb.Mongo object and the default + database name to operate against. @@ -815,8 +816,9 @@ public class AppConfig { MongoTemplate (MongoDbFactory mongoDbFactory) - takes a - MongoDbFactory object that encapsulated the com.mongodb.Mongo - object, database name, and username and password. + MongoDbFactory object that encapsulated the + com.mongodb.Mongo object, database name, and + username and password. @@ -1049,11 +1051,12 @@ DEBUG work.data.mongodb.core.MongoTemplate: 376 - Dropped collection [database.p
How the '_id' field is handled in the mapping layer - Mongo requires that you have an '_id' field for all documents. If - you don't provide one the driver will assign a ObjectId with a generated - value. When using the MongoMappingConverter there - are certain rules that govern how properties from the Java class is - mapped to this '_id' field. + MongoDB requires that you have an '_id' field for all documents. + If you don't provide one the driver will assign a + ObjectId with a generated value. When using the + MongoMappingConverter there are certain rules + that govern how properties from the Java class is mapped to this '_id' + field. The following outlines what property will be mapped to the '_id' document field: @@ -1083,8 +1086,8 @@ DEBUG work.data.mongodb.core.MongoTemplate: 376 - Dropped collection [database.p An id property or field declared as a String in the Java class will be converted to and stored as an ObjectId if possible using a Spring Converter<String, ObjectId>. Valid conversion rules are - delegated to the Mongo Java driver. If it cannot be converted to an - ObjectId, then the value will be stored as a string in the + delegated to the MongoDB Java driver. If it cannot be converted to + an ObjectId, then the value will be stored as a string in the database. @@ -1641,7 +1644,7 @@ import static org.springframework.data.mongodb.core.query.Query.query; Criteria withinCenterSphere (Circle circle) Creates a geospatial criterion using $within - $center operators. This is only available for Mongo 1.7 + $center operators. This is only available for MongoDB 1.7 and higher. @@ -1662,7 +1665,7 @@ import static org.springframework.data.mongodb.core.query.Query.query; Criteria nearSphere (Point point) Creates a geospatial criterion using $nearSphere$center operations. - This is only available for Mongo 1.7 and higher. + This is only available for MongoDB 1.7 and higher. @@ -2245,8 +2248,31 @@ public class PersonWriteConverter implements Converter<Person, DBObject> {
Index and Collection managment - MongoTemplate provides a few methods for managing indexes and - collections. + MongoTemplate provides a few methods for + managing indexes and collections. These are collected into a helper + interface called IndexOperations. You + access these operations by calilng the method + indexOps and pass in either the collection name + or the java.lang.Class of your entity (the collection + name will be derived from the .class either by name or via annotation + metadata). + + The IndexOperations interface is + shown below + + public interface IndexOperations { + + void ensureIndex(IndexDefinition indexDefinition); + + void dropIndex(String name); + + void dropAllIndexes(); + + void resetIndexCache(); + + List<IndexInfo> getIndexInfo(); + +}
Methods for creating an Index @@ -2257,7 +2283,7 @@ public class PersonWriteConverter implements Converter<Person, DBObject> { Creating an index using the MongoTemplate - mongoTemplate.ensureIndex(new Index().on("name",Order.ASCENDING), Person.class); + mongoTemplate.indexOps(Person.class).ensureIndex(new Index().on("name",Order.ASCENDING)); @@ -2274,7 +2300,25 @@ public class PersonWriteConverter implements Converter<Person, DBObject> { the Venue class defined in a previous section, you would declare a geospatial query as shown below - mongoTemplate.ensureIndex(new GeospatialIndex("location"), Venue.class); + mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location")); +
+ +
+ Accessing index information + + The IndexOperations interface has the method getIndexInfo that + returns a list of IndexInfo objects. This contains all the indexes + defined on the collectcion. Here is an example that defines an index on + the Person class that has age property. + + template.indexOps(Person.class).ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP)); + +List<IndexInfo> 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]] +
@@ -2327,7 +2371,7 @@ mongoTemplate.dropCollection("MyNewCollection");
Executing Commands - You can also get at the Mongo driver's DB.command( + You 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's Data Access Exception hierarchy. @@ -2441,8 +2485,8 @@ mongoTemplate.dropCollection("MyNewCollection"); The Spring framework provides exception translation for a wide variety of database and mapping technologies. This has traditionally been - for JDBC and JPA. The Spring support for Mongo extends this feature to the - MongoDB Database by providing an implementation of the + for JDBC and JPA. The Spring support for MongoDB extends this feature to + the MongoDB Database by providing an implementation of the org.springframework.dao.support.PersistenceExceptionTranslator interface. @@ -2476,7 +2520,7 @@ mongoTemplate.dropCollection("MyNewCollection"); greater need in the case of JDBC and JMS than with MongoDB, it still offers a single spot for exception translation and logging to occur. As such, using thexe execute callback is the preferred way to access the - Mongo driver's DB and Collection objects to perform uncommon operations + MongoDB driver's DB and Collection objects to perform uncommon operations that were not exposed as methods on MongoTemplate. @@ -2537,4 +2581,4 @@ mongoTemplate.dropCollection("MyNewCollection"); } });
- \ No newline at end of file +