Search in sources :

Example 11 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 12 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 13 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)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