Example usage for com.mongodb.client MongoCollection updateMany

List of usage examples for com.mongodb.client MongoCollection updateMany

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection updateMany.

Prototype

UpdateResult updateMany(ClientSession clientSession, Bson filter, List<? extends Bson> update);

Source Link

Document

Update all documents in the collection according to the specified arguments.

Usage

From source file:com.bluedragon.mongo.MongoCollectionUpdate.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData data = getNamedParam(argStruct, "data", null);
    if (data == null)
        throwException(_session, "please specify data to update");

    cfData query = getNamedParam(argStruct, "query", null);
    if (query == null)
        throwException(_session, "please specify query to update");

    boolean upsert = getNamedBooleanParam(argStruct, "upsert", false);
    boolean multi = getNamedBooleanParam(argStruct, "multi", false);

    try {//  w  ww.  ja v a 2  s  .c o m

        MongoCollection<Document> col = db.getCollection(collection);
        Document qry = getDocument(query);
        Document dat = getDocument(data);
        long start = System.currentTimeMillis();

        if (multi)
            col.updateMany(qry, dat, new UpdateOptions().upsert(upsert));
        else
            col.updateOne(qry, dat, new UpdateOptions().upsert(upsert));

        _session.getDebugRecorder().execMongo(col, "update", qry, System.currentTimeMillis() - start);

        return cfBooleanData.TRUE;

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.eightkdata.mongowp.client.wrapper.MongoConnectionWrapper.java

License:Open Source License

@Override
public void asyncUpdate(String database, String collection, BsonDocument selector, BsonDocument update,
        boolean upsert, boolean multiUpdate) throws MongoException {

    try {/*from  w  ww  .j av a2s . c  o  m*/
        UpdateOptions updateOptions = new UpdateOptions().upsert(upsert);

        MongoCollection<org.bson.BsonDocument> mongoCollection = owner.getDriverClient().getDatabase(database)
                .getCollection(collection, org.bson.BsonDocument.class);
        org.bson.BsonDocument translatedUpdate = MongoBsonTranslator.translate(update);
        if (multiUpdate) {
            mongoCollection.updateMany(translatedUpdate, translatedUpdate, updateOptions);
        } else {
            mongoCollection.updateOne(translatedUpdate, translatedUpdate, updateOptions);
        }
    } catch (com.mongodb.MongoException ex) { //a general Mongo driver exception
        if (ErrorCode.isErrorCode(ex.getCode())) {
            throw toMongoException(ex);
        } else {
            throw toRuntimeMongoException(ex);
        }
    } catch (IOException ex) {
        throw new BadValueException("Unexpected IO exception", ex);
    }
}

From source file:com.torodb.mongowp.client.wrapper.MongoConnectionWrapper.java

License:Apache License

@Override
public void asyncUpdate(String database, String collection, BsonDocument selector, BsonDocument update,
        boolean upsert, boolean multiUpdate) throws MongoException {

    try {//from  w w  w  .  j  ava  2s .c o m
        UpdateOptions updateOptions = new UpdateOptions().upsert(upsert);

        MongoCollection<org.bson.BsonDocument> mongoCollection = owner.getDriverClient().getDatabase(database)
                .getCollection(collection, org.bson.BsonDocument.class);
        org.bson.BsonDocument translatedUpdate = MongoBsonTranslator.translate(update);
        if (multiUpdate) {
            mongoCollection.updateMany(translatedUpdate, translatedUpdate, updateOptions);
        } else {
            mongoCollection.updateOne(translatedUpdate, translatedUpdate, updateOptions);
        }
    } catch (com.mongodb.MongoException ex) { //a general Mongo driver exception
        if (ErrorCode.isErrorCode(ex.getCode())) {
            throw toMongoException(ex);
        } else {
            throw toRuntimeMongoException(ex);
        }
    }
}