use of com.mongodb.operation.UpdateOperation in project mongo-java-driver by mongodb.
the class DBCollection method update.
/**
* Modify an existing document or documents in collection.
*
* @param query the selection criteria for the update
* @param update the modifications to apply
* @param options the options to apply to the update operation
* @return the result of the operation
* @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key
* @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the update command
* @throws MongoException if the operation failed for some other reason
* @mongodb.driver.manual tutorial/modify-documents/ Modify
* @since 3.4
*/
public WriteResult update(final DBObject query, final DBObject update, final DBCollectionUpdateOptions options) {
notNull("query", query);
notNull("update", update);
notNull("options", options);
WriteConcern writeConcern = options.getWriteConcern() != null ? options.getWriteConcern() : getWriteConcern();
if (!update.keySet().isEmpty() && update.keySet().iterator().next().startsWith("$")) {
UpdateRequest updateRequest = new UpdateRequest(wrap(query), wrap(update, options.getEncoder()), com.mongodb.bulk.WriteRequest.Type.UPDATE).upsert(options.isUpsert()).multi(options.isMulti()).collation(options.getCollation());
return executeWriteOperation(new UpdateOperation(getNamespace(), false, writeConcern, singletonList(updateRequest)).bypassDocumentValidation(options.getBypassDocumentValidation()));
} else {
UpdateRequest replaceRequest = new UpdateRequest(wrap(query), wrap(update, options.getEncoder()), com.mongodb.bulk.WriteRequest.Type.REPLACE).upsert(options.isUpsert()).multi(options.isMulti()).collation(options.getCollation());
return executeWriteOperation(new UpdateOperation(getNamespace(), true, writeConcern, singletonList(replaceRequest)).bypassDocumentValidation(options.getBypassDocumentValidation()));
}
}
use of com.mongodb.operation.UpdateOperation in project mongo-java-driver by mongodb.
the class DBCollection method replaceOrInsert.
@SuppressWarnings("unchecked")
private WriteResult replaceOrInsert(final DBObject obj, final Object id, final WriteConcern writeConcern) {
DBObject filter = new BasicDBObject(ID_FIELD_NAME, id);
UpdateRequest replaceRequest = new UpdateRequest(wrap(filter), wrap(obj, objectCodec), com.mongodb.bulk.WriteRequest.Type.REPLACE).upsert(true);
return executeWriteOperation(new UpdateOperation(getNamespace(), false, writeConcern, asList(replaceRequest)));
}
Aggregations