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!• MongoTemplate (Mongo mongo, String databaseName, UserCredentialsuserCredentials) - adds the username and password for authenticating with the database.• MongoTemplate (MongoDbFactory mongoDbFactory) - takes a MongoDbFactory object thatencapsulated the com.mongodb.Mongo object, database name, and username and password.• MongoTemplate (MongoDbFactory mongoDbFactory, MongoConvertermongoConverter) - adds a MongoConverter to use for mapping.You can also configure a MongoTemplate using Spring's XML schema.Other optional properties that you might like to set when creating a MongoTemplate are the defaultWriteResultCheckingPolicy, WriteConcern, and ReadPreference.NoteThe preferred way to reference the operations on MongoTemplate instance is via its interfaceMongoOperations.WriteResultChecking PolicyWhen in development it is very handy to either log or throw an exception if thecom.mongodb.WriteResult returned from any MongoDB operation contains an error. It is quitecommon to forget to do this during development and then end up with an application that lookslike it runs successfully but in fact the database was not modified according to your expectations.Set MongoTemplate's WriteResultChecking property to an enum with the following values, LOG,EXCEPTION, or NONE to either log the error, throw and exception or do nothing. The default is to usea WriteResultChecking value of NONE.WriteConcernYou can set the com.mongodb.WriteConcern property that the MongoTemplate will use for writeoperations if it has not yet been specified via the driver at a higher level such as com.mongodb.Mongo.If MongoTemplate's WriteConcern property is not set it will default to the one set in the MongoDBdriver's DB or Collection setting.WriteConcernResolverFor more advanced cases where you want to set different WriteConcern values on a peroperationbasis (for remove, update, insert and save operations), a strategy interface calledWriteConcernResolver can be configured on MongoTemplate. Since MongoTemplate is used topersist POJOs, the WriteConcernResolver lets you create a policy that can map a specific POJOclass to a WriteConcern value. The WriteConcernResolver interface is shown below.public interface WriteConcernResolver {WriteConcern resolve(MongoAction action);}1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 35
please define productname in your docbook file!The passed in argument, MongoAction, is what you use to determine the WriteConcern valueto be used or to use the value of the Template itself as a default. MongoAction contains thecollection name being written to, the java.lang.Class of the POJO, the converted DBObject, aswell as the operation as an enumeration (MongoActionOperation: REMOVE, UPDATE, INSERT,INSERT_LIST, SAVE) and a few other pieces of contextual information. For example,private class MyAppWriteConcernResolver implements WriteConcernResolver {}public WriteConcern resolve(MongoAction action) {if (action.getEntityClass().getSimpleName().contains("Audit")) {return WriteConcern.NONE;} else if (action.getEntityClass().getSimpleName().contains("Metadata")) {return WriteConcern.JOURNAL_SAFE;}return action.getDefaultWriteConcern();}5.5 Saving, Updating, and Removing DocumentsMongoTemplate provides a simple way for you to save, update, and delete your domain objects andmap those objects to documents stored in MongoDB.Given a simple class such as Personpublic class Person {private String id;private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getId() {return id;}public String getName() {return name;}public int getAge() {return age;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";}}You can save, update and delete the object as shown below.NoteMongoOperations is the interface that MongoTemplate implements.1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 36
- Page 1 and 2: Spring Data MongoDB - Reference Doc
- Page 3 and 4: please define productname in your d
- Page 5 and 6: please define productname in your d
- Page 7 and 8: please define productname in your d
- Page 9 and 10: please define productname in your d
- Page 11: please define productname in your d
- Page 14 and 15: please define productname in your d
- Page 16 and 17: please define productname in your d
- 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 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 68 and 69: 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
please define productname in your docbook file!• MongoTemplate (Mongo mongo, String databaseName, UserCredentialsuserCredentials) - adds the username and password for authenticating with the database.• MongoTemplate (MongoDbFactory mongoDbFactory) - takes a MongoDbFactory object thatencapsulated the com.mongodb.Mongo object, database name, and username and password.• MongoTemplate (MongoDbFactory mongoDbFactory, MongoConvertermongoConverter) - adds a MongoConverter to use for mapping.You can also configure a MongoTemplate using <strong>Spring</strong>'s XML schema.Other optional properties that you might like to set when creating a MongoTemplate are the defaultWriteResultCheckingPolicy, WriteConcern, and ReadPreference.NoteThe preferred way to reference the operations on MongoTemplate instance is via its interfaceMongoOperations.WriteResultChecking PolicyWhen in development it is very handy to either log or throw an exception if thecom.mongodb.WriteResult returned from any <strong>MongoDB</strong> operation contains an error. It is quitecommon to forget to do this during development and then end up with an application that lookslike it runs successfully but in fact the database was not modified according to your expectations.Set MongoTemplate's WriteResultChecking property to an enum with the following values, LOG,EXCEPTION, or NONE to either log the error, throw and exception or do nothing. The default is to usea WriteResultChecking value of NONE.WriteConcernYou can set the com.mongodb.WriteConcern property that the MongoTemplate will use for writeoperations if it has not yet been specified via the driver at a higher level such as com.mongodb.Mongo.If MongoTemplate's WriteConcern property is not set it will default to the one set in the <strong>MongoDB</strong>driver's DB or Collection setting.WriteConcernResolverFor more advanced cases where you want to set different WriteConcern values on a peroperationbasis (for remove, update, insert and save operations), a strategy interface calledWriteConcernResolver can be configured on MongoTemplate. Since MongoTemplate is used topersist POJOs, the WriteConcernResolver lets you create a policy that can map a specific POJOclass to a WriteConcern value. The WriteConcernResolver interface is shown below.public interface WriteConcernResolver {WriteConcern resolve(MongoAction action);}1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 35