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());
}
};
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class ReplaceAcceptanceTest method shouldInsertTheDocumentIfReplacingWithUpsertAndDocumentNotFoundInCollection.
@Test
public void shouldInsertTheDocumentIfReplacingWithUpsertAndDocumentNotFoundInCollection() {
// given
assertThat(collection.count(), is(0L));
// when
Document replacement = new Document("_id", 3).append("x", 2);
collection.replaceOne(new Document(), replacement, new UpdateOptions().upsert(true));
// then
assertThat(collection.count(), is(1L));
assertThat(collection.find(new Document("_id", 3)).iterator().next(), is(replacement));
}
use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.
the class UpdateAcceptanceTest method shouldInsertTheDocumentIfUpdatingAllWithUpsertAndDocumentNotFoundInCollection.
@Test
public void shouldInsertTheDocumentIfUpdatingAllWithUpsertAndDocumentNotFoundInCollection() {
// given
Document originalDocument = new Document("_id", 1).append("x", 2);
collection.insertOne(originalDocument);
// when
Document filter = new Document("_id", 2);
collection.updateMany(filter, new Document("$set", new Document("x", 5)), new UpdateOptions().upsert(true));
// then
assertThat(collection.count(), is(2L));
Document expectedDocument = new Document("_id", 2).append("x", 5);
assertThat(collection.find(filter).iterator().next(), is(expectedDocument));
}
use of com.mongodb.client.model.UpdateOptions in project sling by apache.
the class MongoDBNoSqlAdapter method store.
@Override
public boolean store(NoSqlData data) {
Document envelope = new Document();
envelope.put(PN_PATH, data.getPath());
envelope.put(PN_DATA, new Document(data.getProperties(MultiValueMode.LISTS)));
// for list-children query efficiency store parent path as well
String parentPath = ResourceUtil.getParent(data.getPath());
if (parentPath != null) {
envelope.put(PN_PARENT_PATH, parentPath);
}
UpdateResult result = collection.replaceOne(Filters.eq(PN_PATH, data.getPath()), envelope, new UpdateOptions().upsert(true));
// return true if a new entry was inserted, false if an existing was replaced
return (result.getMatchedCount() == 0);
}
Aggregations