Search in sources :

Example 6 with UpdateOptions

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));
}
Also used : CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) Test(org.junit.Test)

Example 7 with UpdateOptions

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));
}
Also used : CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) Test(org.junit.Test)

Example 8 with UpdateOptions

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);
    }
}
Also used : DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(com.mongodb.client.model.UpdateOptions) IOException(java.io.IOException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) NoSuchElementException(java.util.NoSuchElementException) Bson(org.bson.conversions.Bson)

Example 9 with UpdateOptions

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());
        }
    };
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Example 10 with UpdateOptions

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());
        }
    };
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Aggregations

UpdateOptions (com.mongodb.client.model.UpdateOptions)13 Document (org.bson.Document)9 Test (org.junit.Test)6 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 CountOptions (com.mongodb.client.model.CountOptions)2 UpdateResult (com.mongodb.client.result.UpdateResult)2 IOException (java.io.IOException)1 NoSuchElementException (java.util.NoSuchElementException)1 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)1 Bson (org.bson.conversions.Bson)1