use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class UpdateAcceptanceTest method shouldUpdateAllDocumentsIfUpdatingAllWithUpsertAndMultipleDocumentsFoundInCollection.
@Test
public void shouldUpdateAllDocumentsIfUpdatingAllWithUpsertAndMultipleDocumentsFoundInCollection() {
// given
Document firstDocument = new Document("_id", 1).append("x", 3);
collection.insertOne(firstDocument);
Document secondDocument = new Document("_id", 2).append("x", 3);
collection.insertOne(secondDocument);
// when
Document filter = new Document("x", 3);
collection.updateMany(filter, new Document("$set", new Document("x", 5)), new UpdateOptions().upsert(true));
// then
assertThat(collection.count(new Document("x", 5), new CountOptions()), is(2L));
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class UpdateAcceptanceTest method shouldUpdateOneDocumentIfUpdatingOneWithUpsertAndMultipleDocumentsFoundInCollection.
@Test
public void shouldUpdateOneDocumentIfUpdatingOneWithUpsertAndMultipleDocumentsFoundInCollection() {
// given
Document firstDocument = new Document("_id", 1).append("x", 3);
collection.insertOne(firstDocument);
Document secondDocument = new Document("_id", 2).append("x", 3);
collection.insertOne(secondDocument);
// when
Document filter = new Document("x", 3);
collection.updateOne(filter, new Document("$set", new Document("x", 5)), new UpdateOptions().upsert(true));
// then
assertThat(collection.count(new Document("x", 5), new CountOptions()), is(1L));
}
use of com.mongodb.client.model.UpdateOptions in project drill by apache.
the class MongoPersistentStore method putIfAbsent.
@Override
public boolean putIfAbsent(String key, V value) {
try {
Bson query = Filters.eq(DrillMongoConstants.ID, key);
Bson update = Updates.set(pKey, bytes(value));
UpdateResult updateResult = collection.updateOne(query, update, new UpdateOptions().upsert(true));
return updateResult.getModifiedCount() == 1;
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new DrillRuntimeException(e.getMessage(), e);
}
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class CrudTest method getUpdateManyMongoOperation.
private MongoOperationUpdateResult getUpdateManyMongoOperation(final BsonDocument arguments) {
return new MongoOperationUpdateResult() {
@Override
public void execute() {
UpdateOptions options = new UpdateOptions();
if (arguments.containsKey("upsert")) {
options.upsert(arguments.getBoolean("upsert").getValue());
}
if (arguments.containsKey("collation")) {
options.collation(getCollation(arguments.getDocument("collation")));
}
collection.updateMany(arguments.getDocument("filter"), arguments.getDocument("update"), options, getCallback());
}
};
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class CrudTest method getReplaceOneMongoOperation.
private MongoOperationUpdateResult getReplaceOneMongoOperation(final BsonDocument arguments) {
return new MongoOperationUpdateResult() {
@Override
public void execute() {
UpdateOptions options = new UpdateOptions();
if (arguments.containsKey("upsert")) {
options.upsert(arguments.getBoolean("upsert").getValue());
}
if (arguments.containsKey("collation")) {
options.collation(getCollation(arguments.getDocument("collation")));
}
collection.replaceOne(arguments.getDocument("filter"), arguments.getDocument("replacement"), options, getCallback());
}
};
}
Aggregations