Search in sources :

Example 46 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project core-ng-project by neowu.

the class MongoCollectionImpl method bulkReplace.

@Override
public void bulkReplace(List<T> entities) {
    StopWatch watch = new StopWatch();
    if (entities == null || entities.isEmpty())
        throw Exceptions.error("entities must not be empty");
    for (T entity : entities) {
        validator.validate(entity);
    }
    try {
        List<ReplaceOneModel<T>> models = new ArrayList<>(entities.size());
        for (T entity : entities) {
            Object id = mongo.codecs.id(entity);
            if (id == null)
                throw Exceptions.error("entity must have id, entityClass={}", entityClass.getCanonicalName());
            models.add(new ReplaceOneModel<>(Filters.eq("_id", id), entity, new UpdateOptions().upsert(true)));
        }
        collection().bulkWrite(models, new BulkWriteOptions().ordered(false));
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("mongoDB", elapsedTime, 0, entities.size());
        logger.debug("bulkReplace, collection={}, size={}, elapsedTime={}", collectionName, entities.size(), elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) UpdateOptions(com.mongodb.client.model.UpdateOptions) StopWatch(core.framework.util.StopWatch)

Example 47 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project core-ng-project by neowu.

the class MongoCollectionImpl method replace.

@Override
public void replace(T entity) {
    StopWatch watch = new StopWatch();
    Object id = null;
    validator.validate(entity);
    try {
        id = mongo.codecs.id(entity);
        if (id == null)
            throw Exceptions.error("entity must have id, entityClass={}", entityClass.getCanonicalName());
        collection().replaceOne(Filters.eq("_id", id), entity, new UpdateOptions().upsert(true));
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("mongoDB", elapsedTime, 0, 1);
        logger.debug("replace, collection={}, id={}, elapsedTime={}", collectionName, id, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : UpdateOptions(com.mongodb.client.model.UpdateOptions) StopWatch(core.framework.util.StopWatch)

Example 48 with UpdateOptions

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

the class MongoDbStorage method replaceDocument.

@Override
public void replaceDocument(final String collectionName, final DataDocument replaceDocument, final DataFilter filter) {
    DataDocument toReplace = new DataDocument(replaceDocument);
    if (toReplace.containsKey(DOCUMENT_ID)) {
        toReplace.remove(DOCUMENT_ID);
    }
    Document replaceDoc = new Document(toReplace);
    database.getCollection(collectionName).replaceOne(filter.<Bson>get(), replaceDoc, new UpdateOptions().upsert(true));
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) BsonDocument(org.bson.BsonDocument) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

Example 49 with UpdateOptions

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

the class MongoUserDao method deleteGroupFromUsers.

@Override
public void deleteGroupFromUsers(final String organizationId, final String group) {
    String key = MongoUtils.concatParams(UserCodec.ALL_GROUPS, "$[" + ELEMENT_NAME + "]", UserCodec.GROUPS);
    Bson pullGroups = Updates.pull(key, group);
    UpdateOptions options = new UpdateOptions().arrayFilters(arrayFilters(organizationId));
    databaseCollection().updateMany(new BsonDocument(), pullGroups, options);
}
Also used : BsonDocument(org.bson.BsonDocument) UpdateOptions(com.mongodb.client.model.UpdateOptions) Bson(org.bson.conversions.Bson)

Example 50 with UpdateOptions

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

the class MongoStore method performPut.

/**
 * Update a object that already exists in the store. The object must exist
 * already or the update may fail.
 *
 * @param key
 *          identifier of the object in the store
 * @param obj
 *          the object to be inserted
 */
private void performPut(final K key, final T obj) {
    // Build the query to select the object to be updated
    Document qSel = new Document("_id", key);
    // Build the update query
    Document qUpdate = new Document();
    Document qUpdateSet = newUpdateSetInstance(obj);
    if (qUpdateSet.size() > 0) {
        qUpdate.put("$set", qUpdateSet);
    }
    Document qUpdateUnset = newUpdateUnsetInstance(obj);
    if (qUpdateUnset.size() > 0) {
        qUpdate.put("$unset", qUpdateUnset);
    }
    // Execute the update (if there is at least one $set ot $unset
    if (!qUpdate.isEmpty()) {
        mongoClientColl.updateOne(qSel, qUpdate, new UpdateOptions().upsert(true));
        obj.clearDirty();
    } else {
        LOG.debug("No update to perform, skip {}", key);
    }
}
Also used : Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions)

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