13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

please define productname in your docbook file!Inserting several objects in a batchThe <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 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 <strong>MongoDB</strong>.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-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 42

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

Saved successfully!

Ooh no, something went wrong!