Search in sources :

Example 26 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 27 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)

Example 28 with UpdateOptions

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

Example 29 with UpdateOptions

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

Example 30 with UpdateOptions

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

Aggregations

UpdateOptions (com.mongodb.client.model.UpdateOptions)53 Document (org.bson.Document)31 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)21 Bson (org.bson.conversions.Bson)17 UpdateResult (com.mongodb.client.result.UpdateResult)10 Test (org.junit.jupiter.api.Test)10 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)8 ArrayList (java.util.ArrayList)6 BsonDocument (org.bson.BsonDocument)6 Test (org.junit.Test)6 Update (org.springframework.data.mongodb.core.query.Update)4 BasicDBObject (com.mongodb.BasicDBObject)3 MongoCollection (com.mongodb.client.MongoCollection)3 CountOptions (com.mongodb.client.model.CountOptions)3 IOException (java.io.IOException)3 List (java.util.List)3 BsonValue (org.bson.BsonValue)3 ObjectId (org.bson.types.ObjectId)3 MongodbException (com.duangframework.core.exceptions.MongodbException)2 Block (com.mongodb.Block)2