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!The simple case of using the save operation is to save a POJO. In this case the collection name will bedetermined by name (not fully qualfied) of the class. You may also call the save operation with a specificcollection name. The collection to store the object can be overriden using mapping metadata.When inserting or saving, if the Id property is not set, the assumption is that its value will beautogenerated by the database. As such, for autogeneration of an ObjectId to succeed the type of theId property/field in your class must be either a String, ObjectId, or BigInteger.Here is a basic example of using the save operation and retrieving its contents.import static org.springframework.data.mongodb.core.query.Criteria.where;import static org.springframework.data.mongodb.core.query.Criteria.query;…Person p = new Person("Bob", 33);mongoTemplate.insert(p);Person qp = mongoTemplate.findOne(query(where("age").is(33)), Person.class);Example 5.11 Inserting and retrieving documents using the MongoTemplateThe insert/save operations available to you are listed below.• void save (Object objectToSave) Save the object to the default collection.• void save (Object objectToSave, String collectionName) Save the object to thespecified collection.A similar set of insert operations is listed below• void insert (Object objectToSave) Insert the object to the default collection.• void insert (Object objectToSave, String collectionName) Insert the object to thespecified collection.Which collection will my documents be saved into?There are two ways to manage the collection name that is used for operating on the documents.The default collection name that is used is the class name changed to start with a lower-case letter.So a com.test.Person class would be stored in the "person" collection. You can customize thisby providing a different collection name using the @Document annotation. You can also overridethe collection name by providing your own collection name as the last parameter for the selectedMongoTemplate method calls.Inserting or saving individual objectsThe MongoDB driver supports inserting a collection of documents in one operation. The methods in theMongoOperations interface that support this functionality are listed below• insert Insert an object. If there is an existing document with the same id then an error is generated.• insertAll Takes a Collection of objects as the first parameter. This method ispects each objectand inserts it to the appropriate collection based on the rules specified above.• save Save the object ovewriting any object that might exist with the same id.1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 41
please define productname in your docbook file!Inserting several objects in a batchThe MongoDB driver supports inserting a collection of documents in one operation. The methods in theMongoOperations interface that support this functionality are listed below• insert methods that take a Collection as the first argument.This inserts a list ofobjects in a single batch write to the database.Updating documents in a collectionFor updates we can elect to update the first document found using MongoOperation's methodupdateFirst or we can update all documents that were found to match the query using the methodupdateMulti. Here is an example of an update of all SAVINGS accounts where we are adding a onetime $50.00 bonus to the balance using the $inc operator.import static org.springframework.data.mongodb.core.query.Criteria.where;import static org.springframework.data.mongodb.core.query.Query;import static org.springframework.data.mongodb.core.query.Update;...WriteResult wr = mongoTemplate.updateMulti(newQuery(where("accounts.accountType").is(Account.Type.SAVINGS)),new Update().inc("accounts.$.balance", 50.00),Account.class);Example 5.12 Updating documents using the MongoTemplateIn addition to the Query discussed above we provide the update definition using an Update object. TheUpdate class has methods that match the update modifiers available for MongoDB.As you can see most methods return the Update object to provide a fluent style for the API.Methods for executing updates for documents• updateFirst Updates the first document that matches the query document criteria with the providedupdated document.• updateMulti Updates all objects that match the query document criteria with the provided updateddocument.Methods for the Update classThe Update class can be used with a little 'syntax sugar' as its methods are meant to be chained togetherand you can kickstart the creation of a new Update instance via the static method public staticUpdate update(String key, Object value) and using static imports.Here is a listing of methods on the Update class• Update addToSet (String key, Object value) Update using the $addToSet updatemodifier• Update inc (String key, Number inc) Update using the $inc update modifier1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 42
- 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 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 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
- 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
please define productname in your docbook file!The simple case of using the save operation is to save a POJO. In this case the collection name will bedetermined by name (not fully qualfied) of the class. You may also call the save operation with a specificcollection name. The collection to store the object can be overriden using mapping metadata.When inserting or saving, if the Id property is not set, the assumption is that its value will beautogenerated by the database. As such, for autogeneration of an ObjectId to succeed the type of theId property/field in your class must be either a String, ObjectId, or BigInteger.Here is a basic example of using the save operation and retrieving its contents.import static org.springframework.data.mongodb.core.query.Criteria.where;import static org.springframework.data.mongodb.core.query.Criteria.query;…Person p = new Person("Bob", 33);mongoTemplate.insert(p);Person qp = mongoTemplate.findOne(query(where("age").is(33)), Person.class);Example 5.11 Inserting and retrieving documents using the MongoTemplateThe insert/save operations available to you are listed below.• void save (Object objectToSave) Save the object to the default collection.• void save (Object objectToSave, String collectionName) Save the object to thespecified collection.A similar set of insert operations is listed below• void insert (Object objectToSave) Insert the object to the default collection.• void insert (Object objectToSave, String collectionName) Insert the object to thespecified collection.Which collection will my documents be saved into?There are two ways to manage the collection name that is used for operating on the documents.The default collection name that is used is the class name changed to start with a lower-case letter.So a com.test.Person class would be stored in the "person" collection. You can customize thisby providing a different collection name using the @Document annotation. You can also overridethe collection name by providing your own collection name as the last parameter for the selectedMongoTemplate method calls.Inserting or saving individual objectsThe <strong>MongoDB</strong> driver supports inserting a collection of documents in one operation. The methods in theMongoOperations interface that support this functionality are listed below• insert Insert an object. If there is an existing document with the same id then an error is generated.• insertAll Takes a Collection of objects as the first parameter. This method ispects each objectand inserts it to the appropriate collection based on the rules specified above.• save Save the object ovewriting any object that might exist with the same id.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 41