Example usage for com.mongodb.client.model FindOneAndUpdateOptions sort

List of usage examples for com.mongodb.client.model FindOneAndUpdateOptions sort

Introduction

In this page you can find the example usage for com.mongodb.client.model FindOneAndUpdateOptions sort.

Prototype

Bson sort

To view the source code for com.mongodb.client.model FindOneAndUpdateOptions sort.

Click Source Link

Usage

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

License:Open Source License

@SuppressWarnings("rawtypes")
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 update = getNamedParam(argStruct, "update", null);
    if (update == null)
        throwException(_session, "please specify update");

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

    try {/* w ww . ja  va  2  s  .  co m*/

        MongoCollection<Document> col = db.getCollection(collection);
        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();

        if (getNamedParam(argStruct, "fields", null) != null)
            findOneAndUpdateOptions.projection(getDocument(getNamedParam(argStruct, "fields", null)));

        if (getNamedParam(argStruct, "sort", null) != null)
            findOneAndUpdateOptions.sort(getDocument(getNamedParam(argStruct, "sort", null)));

        findOneAndUpdateOptions.upsert(getNamedBooleanParam(argStruct, "upsert", false));

        if (getNamedBooleanParam(argStruct, "returnnew", false))
            findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Document qry = getDocument(query);
        long start = System.currentTimeMillis();

        Document result = col.findOneAndUpdate(qry, getDocument(update), findOneAndUpdateOptions);

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

        return tagUtils.convertToCfData((Map) result);

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

From source file:io.vertx.ext.mongo.impl.MongoClientImpl.java

License:Open Source License

@Override
public io.vertx.ext.mongo.MongoClient findOneAndUpdateWithOptions(String collection, JsonObject query,
        JsonObject update, FindOptions findOptions, UpdateOptions updateOptions,
        Handler<AsyncResult<JsonObject>> resultHandler) {
    requireNonNull(collection, "collection cannot be null");
    requireNonNull(query, "query cannot be null");
    requireNonNull(findOptions, "find options cannot be null");
    requireNonNull(updateOptions, "update options cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);

    Bson bquery = wrap(encodedQuery);/*  www. ja va 2 s .  c  o m*/
    Bson bupdate = wrap(update);
    FindOneAndUpdateOptions foauOptions = new FindOneAndUpdateOptions();
    foauOptions.sort(wrap(findOptions.getSort()));
    foauOptions.projection(wrap(findOptions.getFields()));
    foauOptions.upsert(updateOptions.isUpsert());
    foauOptions.returnDocument(
            updateOptions.isReturningNewDocument() ? ReturnDocument.AFTER : ReturnDocument.BEFORE);

    MongoCollection<JsonObject> coll = getCollection(collection);
    coll.findOneAndUpdate(bquery, bupdate, foauOptions, wrapCallback(resultHandler));
    return this;
}