Search in sources :

Example 6 with FindOneAndReplaceOptions

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

the class CrudTest method getFindOneAndReplaceMongoOperation.

private MongoOperationBsonDocument getFindOneAndReplaceMongoOperation(final BsonDocument arguments) {
    return new MongoOperationBsonDocument() {

        @Override
        public void execute() {
            FindOneAndReplaceOptions options = new FindOneAndReplaceOptions();
            if (arguments.containsKey("projection")) {
                options.projection(arguments.getDocument("projection"));
            }
            if (arguments.containsKey("sort")) {
                options.sort(arguments.getDocument("sort"));
            }
            if (arguments.containsKey("upsert")) {
                options.upsert(arguments.getBoolean("upsert").getValue());
            }
            if (arguments.containsKey("returnDocument")) {
                options.returnDocument(arguments.getString("returnDocument").getValue().equals("After") ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
            }
            if (arguments.containsKey("collation")) {
                options.collation(getCollation(arguments.getDocument("collation")));
            }
            collection.findOneAndReplace(arguments.getDocument("filter"), arguments.getDocument("replacement"), options, getCallback());
        }
    };
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions)

Example 7 with FindOneAndReplaceOptions

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

the class FindAndReplaceAcceptanceTest method shouldReturnNewDocumentAfterReplaceWhenUsingReplaceOneAndGet.

@Test
public void shouldReturnNewDocumentAfterReplaceWhenUsingReplaceOneAndGet() {
    ObjectId id = new ObjectId();
    Document documentInserted = new Document("_id", id).append(KEY, VALUE_TO_CARE_ABOUT);
    Document documentReplacement = new Document("_id", id).append("foo", "bar");
    collection.insertOne(documentInserted);
    assertThat(collection.count(), is(1L));
    Document document = collection.findOneAndReplace(new Document(KEY, VALUE_TO_CARE_ABOUT), documentReplacement, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
    assertThat("Document, retrieved from replaceAndGet after change applied should match the document used as replacement", document, equalTo(documentReplacement));
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Test(org.junit.Test)

Example 8 with FindOneAndReplaceOptions

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

the class MongoCollectionImplTest method testFindOneAndReplace.

@Test
public void testFindOneAndReplace() {
    FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().collation(collation);
    Document replacement = new Document();
    assertAll("findOneAndReplace", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(null, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(clientSession, null, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(clientSession, filter, replacement, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(null, filter, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndReplace(null, filter, replacement, options))), () -> {
        Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(null, filter, replacement, new FindOneAndReplaceOptions());
        assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(filter, replacement), "Default");
    }, () -> {
        Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(null, filter, replacement, options);
        assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(filter, replacement, options), "With filter & options");
    }, () -> {
        Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions());
        assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(clientSession, filter, replacement), "With client session");
    }, () -> {
        Publisher<Document> expected = mongoOperationPublisher.findOneAndReplace(clientSession, filter, replacement, options);
        assertPublisherIsTheSameAs(expected, collection.findOneAndReplace(clientSession, filter, replacement, options), "With client session, filter & options");
    });
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 9 with FindOneAndReplaceOptions

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

the class JsonPoweredCrudTestHelper method getFindOneAndReplaceResult.

BsonDocument getFindOneAndReplaceResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    FindOneAndReplaceOptions options = new FindOneAndReplaceOptions();
    if (arguments.containsKey("projection")) {
        options.projection(arguments.getDocument("projection"));
    }
    if (arguments.containsKey("sort")) {
        options.sort(arguments.getDocument("sort"));
    }
    if (arguments.containsKey("upsert")) {
        options.upsert(arguments.getBoolean("upsert").getValue());
    }
    if (arguments.containsKey("returnDocument")) {
        options.returnDocument(arguments.getString("returnDocument").getValue().equals("After") ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
    }
    if (arguments.containsKey("collation")) {
        options.collation(getCollation(arguments.getDocument("collation")));
    }
    if (arguments.containsKey("hint")) {
        if (arguments.isDocument("hint")) {
            options.hint(arguments.getDocument("hint"));
        } else {
            options.hintString(arguments.getString("hint").getValue());
        }
    }
    BsonDocument result;
    if (clientSession == null) {
        result = getCollection(collectionOptions).findOneAndReplace(arguments.getDocument("filter"), arguments.getDocument("replacement"), options);
    } else {
        result = getCollection(collectionOptions).findOneAndReplace(clientSession, arguments.getDocument("filter"), arguments.getDocument("replacement"), options);
    }
    return toResult(result);
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) BsonDocument(org.bson.BsonDocument)

Example 10 with FindOneAndReplaceOptions

use of com.mongodb.client.model.FindOneAndReplaceOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method findAndReplaceShouldUseCollationWhenPresent.

// DATAMONGO-1854
@Test
void findAndReplaceShouldUseCollationWhenPresent() {
    template.findAndReplace(new BasicQuery("{}").collation(Collation.of("fr")), new Jedi()).subscribe();
    ArgumentCaptor<FindOneAndReplaceOptions> options = ArgumentCaptor.forClass(FindOneAndReplaceOptions.class);
    verify(collection).findOneAndReplace(any(Bson.class), any(), options.capture());
    assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Aggregations

FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)13 Document (org.bson.Document)5 ReturnDocument (com.mongodb.client.model.ReturnDocument)4 Test (org.junit.jupiter.api.Test)4 BsonDocument (org.bson.BsonDocument)3 ObjectId (org.bson.types.ObjectId)3 Test (org.junit.Test)3 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)3 StorageException (io.lumeer.storage.api.exception.StorageException)2 Bson (org.bson.conversions.Bson)2 MongoException (com.mongodb.MongoException)1 Worker (com.mongodb.client.test.Worker)1 WorkerCodecProvider (com.mongodb.client.test.WorkerCodecProvider)1 JsonView (io.lumeer.api.dto.JsonView)1 View (io.lumeer.api.model.View)1 DataDocument (io.lumeer.engine.api.data.DataDocument)1 Date (java.util.Date)1 Map (java.util.Map)1 BsonString (org.bson.BsonString)1 BsonValue (org.bson.BsonValue)1