Search in sources :

Example 1 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project zeppelin by apache.

the class MongoNotebookRepo method save.

@Override
public void save(Note note, AuthenticationInfo subject) throws IOException {
    Document doc = noteToDocument(note);
    coll.replaceOne(eq("_id", note.getId()), doc, new UpdateOptions().upsert(true));
}
Also used : Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Example 2 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.

the class CrudTest method getUpdateOneMongoOperation.

private MongoOperationUpdateResult getUpdateOneMongoOperation(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.updateOne(arguments.getDocument("filter"), arguments.getDocument("update"), options, getCallback());
        }
    };
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Example 3 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.

the class ReplaceAcceptanceTest method shouldReplaceTheDocumentIfReplacingWithUpsertAndDocumentIsFoundInCollection.

@Test
public void shouldReplaceTheDocumentIfReplacingWithUpsertAndDocumentIsFoundInCollection() {
    // given
    Document originalDocument = new Document("_id", 3).append("x", 2);
    collection.replaceOne(new Document(), originalDocument, new UpdateOptions().upsert(true));
    assertThat(collection.count(), is(1L));
    // when
    Document replacement = originalDocument.append("y", 5);
    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 4 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project mongo-java-driver by mongodb.

the class UpdateAcceptanceTest method shouldInsertTheDocumentIfUpdatingOneWithUpsertAndDocumentNotFoundInCollection.

@Test
public void shouldInsertTheDocumentIfUpdatingOneWithUpsertAndDocumentNotFoundInCollection() {
    // given
    Document originalDocument = new Document("_id", 1).append("x", 2);
    collection.insertOne(originalDocument);
    // when
    Document filter = new Document("_id", 2);
    collection.updateOne(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 5 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)

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