Search in sources :

Example 36 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project java-design-patterns by iluwatar.

the class MongoDb method upsertDb.

/**
 * Update data if exists.
 *
 * @param userAccount {@link UserAccount}
 * @return {@link UserAccount}
 */
@Override
public UserAccount upsertDb(final UserAccount userAccount) {
    String userId = userAccount.getUserId();
    String userName = userAccount.getUserName();
    String additionalInfo = userAccount.getAdditionalInfo();
    db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(new Document(USER_ID, userId), new Document("$set", new Document(USER_ID, userId).append(USER_NAME, userName).append(ADD_INFO, additionalInfo)), new UpdateOptions().upsert(true));
    return userAccount;
}
Also used : Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Example 37 with UpdateOptions

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

the class UnifiedCrudHelper method executeUpdateOne.

OperationResult executeUpdateOne(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    BsonDocument filter = arguments.getDocument("filter");
    BsonValue update = arguments.get("update");
    UpdateOptions options = getUpdateOptions(arguments);
    return resultOf(() -> {
        UpdateResult updateResult;
        if (session == null) {
            updateResult = update.isArray() ? collection.updateOne(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(filter, update.asDocument(), options);
        } else {
            updateResult = update.isArray() ? collection.updateOne(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(session, filter, update.asDocument(), options);
        }
        return toExpected(updateResult);
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult) BsonValue(org.bson.BsonValue)

Example 38 with UpdateOptions

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

the class JsonPoweredCrudTestHelper method getUpdateOneResult.

@SuppressWarnings("unchecked")
BsonDocument getUpdateOneResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    UpdateOptions options = new UpdateOptions();
    if (arguments.containsKey("upsert")) {
        options.upsert(arguments.getBoolean("upsert").getValue());
    }
    if (arguments.containsKey("collation")) {
        options.collation(getCollation(arguments.getDocument("collation")));
    }
    if (arguments.containsKey("arrayFilters")) {
        options.arrayFilters((getListOfDocuments(arguments.getArray("arrayFilters"))));
    }
    if (arguments.containsKey("bypassDocumentValidation")) {
        options.bypassDocumentValidation(arguments.getBoolean("bypassDocumentValidation").getValue());
    }
    if (arguments.containsKey("hint")) {
        if (arguments.isDocument("hint")) {
            options.hint(arguments.getDocument("hint"));
        } else {
            options.hintString(arguments.getString("hint").getValue());
        }
    }
    UpdateResult updateResult;
    if (clientSession == null) {
        if (arguments.isDocument("update")) {
            updateResult = getCollection(collectionOptions).updateOne(arguments.getDocument("filter"), arguments.getDocument("update"), options);
        } else {
            // update is a pipeline
            updateResult = getCollection(collectionOptions).updateOne(arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
        }
    } else {
        if (arguments.isDocument("update")) {
            updateResult = getCollection(collectionOptions).updateOne(clientSession, arguments.getDocument("filter"), arguments.getDocument("update"), options);
        } else {
            // update is a pipeline
            updateResult = getCollection(collectionOptions).updateOne(clientSession, arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
        }
    }
    return toResult(updateResult);
}
Also used : UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 39 with UpdateOptions

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

the class UnifiedCrudHelper method executeUpdateMany.

OperationResult executeUpdateMany(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument filter = arguments.getDocument("filter");
    BsonValue update = arguments.get("update");
    ClientSession session = getSession(arguments);
    UpdateOptions options = getUpdateOptions(arguments);
    return resultOf(() -> {
        if (session == null) {
            return update.isArray() ? toExpected(collection.updateMany(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options)) : toExpected(collection.updateMany(filter, update.asDocument(), options));
        } else {
            return update.isArray() ? toExpected(collection.updateMany(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options)) : toExpected(collection.updateMany(session, filter, update.asDocument(), options));
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BsonValue(org.bson.BsonValue)

Example 40 with UpdateOptions

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

the class MongoTemplateUnitTests method updateShouldApplyArrayFilters.

// DATAMONGO-2215
@Test
void updateShouldApplyArrayFilters() {
    template.updateFirst(new BasicQuery("{}"), new Update().set("grades.$[element]", 100).filterArray(Criteria.where("element").gte(100)), EntityWithListOfSimple.class);
    ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
    verify(collection).updateOne(any(), any(Bson.class), options.capture());
    Assertions.assertThat((List<Bson>) options.getValue().getArrayFilters()).contains(new org.bson.Document("element", new Document("$gte", 100)));
}
Also used : Document(org.bson.Document) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) ArrayList(java.util.ArrayList) List(java.util.List) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.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