Example usage for org.springframework.data.mongodb.core MongoActionOperation UPDATE

List of usage examples for org.springframework.data.mongodb.core MongoActionOperation UPDATE

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoActionOperation UPDATE.

Prototype

MongoActionOperation UPDATE

To view the source code for org.springframework.data.mongodb.core MongoActionOperation UPDATE.

Click Source Link

Usage

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected WriteResult doUpdate(final String collectionName, final Query query, final Update update,
        final Class<?> entityClass, final boolean upsert, final boolean multi) {

    return execute(collectionName, new CollectionCallback<WriteResult>() {
        public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException {

            MongoPersistentEntity<?> entity = entityClass == null ? null : getPersistentEntity(entityClass);

            DBObject queryObj = query == null ? new BasicDBObject()
                    : mapper.getMappedObject(query.getQueryObject(), entity);
            DBObject updateObj = update.getUpdateObject();

            for (String key : updateObj.keySet()) {
                updateObj.put(key, mongoConverter.convertToMongoType(updateObj.get(key)));
            }/*from  w  w w.j a va 2  s. co m*/

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("calling update using query: " + queryObj + " and update: " + updateObj
                        + " in collection: " + collectionName);
            }

            WriteResult wr;
            MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName,
                    entityClass, updateObj, queryObj);
            WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
            if (writeConcernToUse == null) {
                wr = collection.update(queryObj, updateObj, upsert, multi);
            } else {
                wr = collection.update(queryObj, updateObj, upsert, multi, writeConcernToUse);
            }
            handleAnyWriteResultErrors(wr, queryObj, "update with '" + updateObj + "'");
            return wr;
        }
    });
}