use of com.mongodb.client.model.UpdateOptions in project java-design-patterns by iluwatar.
the class MongoDb method upsertDb.
/**
* Update data if exists.
*
* @param userAccount {@link UserAccount}
* @return {@link UserAccount}
*/
@Override
public UserAccount upsertDb(final UserAccount userAccount) {
String userId = userAccount.getUserId();
String userName = userAccount.getUserName();
String additionalInfo = userAccount.getAdditionalInfo();
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(new Document(USER_ID, userId), new Document("$set", new Document(USER_ID, userId).append(USER_NAME, userName).append(ADD_INFO, additionalInfo)), new UpdateOptions().upsert(true));
return userAccount;
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeUpdateOne.
OperationResult executeUpdateOne(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
ClientSession session = getSession(arguments);
BsonDocument filter = arguments.getDocument("filter");
BsonValue update = arguments.get("update");
UpdateOptions options = getUpdateOptions(arguments);
return resultOf(() -> {
UpdateResult updateResult;
if (session == null) {
updateResult = update.isArray() ? collection.updateOne(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(filter, update.asDocument(), options);
} else {
updateResult = update.isArray() ? collection.updateOne(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(session, filter, update.asDocument(), options);
}
return toExpected(updateResult);
});
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getUpdateOneResult.
@SuppressWarnings("unchecked")
BsonDocument getUpdateOneResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
UpdateOptions options = new UpdateOptions();
if (arguments.containsKey("upsert")) {
options.upsert(arguments.getBoolean("upsert").getValue());
}
if (arguments.containsKey("collation")) {
options.collation(getCollation(arguments.getDocument("collation")));
}
if (arguments.containsKey("arrayFilters")) {
options.arrayFilters((getListOfDocuments(arguments.getArray("arrayFilters"))));
}
if (arguments.containsKey("bypassDocumentValidation")) {
options.bypassDocumentValidation(arguments.getBoolean("bypassDocumentValidation").getValue());
}
if (arguments.containsKey("hint")) {
if (arguments.isDocument("hint")) {
options.hint(arguments.getDocument("hint"));
} else {
options.hintString(arguments.getString("hint").getValue());
}
}
UpdateResult updateResult;
if (clientSession == null) {
if (arguments.isDocument("update")) {
updateResult = getCollection(collectionOptions).updateOne(arguments.getDocument("filter"), arguments.getDocument("update"), options);
} else {
// update is a pipeline
updateResult = getCollection(collectionOptions).updateOne(arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
}
} else {
if (arguments.isDocument("update")) {
updateResult = getCollection(collectionOptions).updateOne(clientSession, arguments.getDocument("filter"), arguments.getDocument("update"), options);
} else {
// update is a pipeline
updateResult = getCollection(collectionOptions).updateOne(clientSession, arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
}
}
return toResult(updateResult);
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeUpdateMany.
OperationResult executeUpdateMany(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
BsonDocument filter = arguments.getDocument("filter");
BsonValue update = arguments.get("update");
ClientSession session = getSession(arguments);
UpdateOptions options = getUpdateOptions(arguments);
return resultOf(() -> {
if (session == null) {
return update.isArray() ? toExpected(collection.updateMany(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options)) : toExpected(collection.updateMany(filter, update.asDocument(), options));
} else {
return update.isArray() ? toExpected(collection.updateMany(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options)) : toExpected(collection.updateMany(session, filter, update.asDocument(), options));
}
});
}
use of com.mongodb.client.model.UpdateOptions in project spring-data-mongodb by spring-projects.
the class MongoTemplateUnitTests method updateShouldApplyArrayFilters.
// DATAMONGO-2215
@Test
void updateShouldApplyArrayFilters() {
template.updateFirst(new BasicQuery("{}"), new Update().set("grades.$[element]", 100).filterArray(Criteria.where("element").gte(100)), EntityWithListOfSimple.class);
ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
verify(collection).updateOne(any(), any(Bson.class), options.capture());
Assertions.assertThat((List<Bson>) options.getValue().getArrayFilters()).contains(new org.bson.Document("element", new Document("$gte", 100)));
}
Aggregations