Search in sources :

Example 6 with FindOneAndUpdateOptions

use of com.mongodb.client.model.FindOneAndUpdateOptions in project engine by Lumeer.

the class MongoDataDao method patchData.

@Override
public DataDocument patchData(final String collectionId, final String documentId, final DataDocument data) {
    data.remove(ID);
    Document updateDocument = new Document("$set", new Document(data));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
    Document patchedDocument = dataCollection(collectionId).findOneAndUpdate(idFilter(documentId), updateDocument, options);
    if (patchedDocument == null) {
        throw new StorageException("Document '" + documentId + "' has not been patched (partially updated).");
    }
    return MongoUtils.convertDocument(patchedDocument);
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) StorageException(io.lumeer.storage.api.exception.StorageException)

Example 7 with FindOneAndUpdateOptions

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

the class UnifiedCrudHelper method executeFindOneAndUpdate.

OperationResult executeFindOneAndUpdate(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument filter = arguments.getDocument("filter").asDocument();
    BsonValue update = arguments.get("update");
    ClientSession session = getSession(arguments);
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "filter":
            case "update":
            case "session":
                break;
            case "returnDocument":
                switch(cur.getValue().asString().getValue()) {
                    case "Before":
                        options.returnDocument(ReturnDocument.BEFORE);
                        break;
                    case "After":
                        options.returnDocument(ReturnDocument.AFTER);
                        break;
                    default:
                        throw new UnsupportedOperationException("Can't happen");
                }
                break;
            case "hint":
                if (cur.getValue().isString()) {
                    options.hintString(cur.getValue().asString().getValue());
                } else {
                    options.hint(cur.getValue().asDocument());
                }
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            return update.isArray() ? collection.findOneAndUpdate(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.findOneAndUpdate(filter, update.asDocument(), options);
        } else {
            return update.isArray() ? collection.findOneAndUpdate(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.findOneAndUpdate(session, filter, update.asDocument(), options);
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 8 with FindOneAndUpdateOptions

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

the class MongoCollectionImplTest method testFindOneAndUpdate.

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

Example 9 with FindOneAndUpdateOptions

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

the class MongoTemplateUnitTests method findAndModfiyShoudUseCollationWhenPresent.

// DATAMONGO-1518
@Test
void findAndModfiyShoudUseCollationWhenPresent() {
    template.findAndModify(new BasicQuery("{}").collation(Collation.of("fr")), new Update(), AutogenerateableId.class);
    ArgumentCaptor<FindOneAndUpdateOptions> options = ArgumentCaptor.forClass(FindOneAndUpdateOptions.class);
    verify(collection).findOneAndUpdate(any(), any(Bson.class), options.capture());
    assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Update(org.springframework.data.mongodb.core.query.Update) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 10 with FindOneAndUpdateOptions

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

the class MongoTemplateUnitTests method findAndModfiyShoudUseDefaultCollationWhenPresent.

// DATAMONGO-1854
@Test
void findAndModfiyShoudUseDefaultCollationWhenPresent() {
    template.findAndModify(new BasicQuery("{}"), new Update(), Sith.class);
    ArgumentCaptor<FindOneAndUpdateOptions> options = ArgumentCaptor.forClass(FindOneAndUpdateOptions.class);
    verify(collection).findOneAndUpdate(any(), any(Bson.class), options.capture());
    assertThat(options.getValue().getCollation()).isEqualTo(com.mongodb.client.model.Collation.builder().locale("de_AT").build());
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Update(org.springframework.data.mongodb.core.query.Update) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Aggregations

FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)16 Document (org.bson.Document)9 Bson (org.bson.conversions.Bson)7 ReturnDocument (com.mongodb.client.model.ReturnDocument)6 Test (org.junit.jupiter.api.Test)6 BsonDocument (org.bson.BsonDocument)5 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)5 Update (org.springframework.data.mongodb.core.query.Update)5 DataDocument (io.lumeer.engine.api.data.DataDocument)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)2 Stopwatch (com.google.common.base.Stopwatch)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BasicDBObject (com.mongodb.BasicDBObject)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 MongoException (com.mongodb.MongoException)1 ClientSession (com.mongodb.client.ClientSession)1 UpdateResult (com.mongodb.client.result.UpdateResult)1