Example usage for com.mongodb.async.client MongoCollection updateOne

List of usage examples for com.mongodb.async.client MongoCollection updateOne

Introduction

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

Prototype

void updateOne(Bson filter, List<? extends Bson> update, SingleResultCallback<UpdateResult> callback);

Source Link

Document

Update a single document in the collection according to the specified arguments.

Usage

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void removeDocumentField(final String collectionName, Criteria criteria, String field,
        final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.updateOne(criteria.getRestrictions(), unset(field), new SingleResultCallback<UpdateResult>() {
        @Override/*w ww  .j  av a 2 s  .  co  m*/
        public void onResult(final UpdateResult result, final Throwable t) {
            onUpdateResultAction(result, t,
                    result.getModifiedCount() + " " + collectionName + " entity successfully updated",
                    callback);
        }
    });

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void updateDocumentField(final String collectionName, Criteria criteria, final String field,
        Object value, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.updateOne(criteria.getRestrictions(), set(field, value),
            new SingleResultCallback<UpdateResult>() {
                @Override/*from  w  w  w . ja  v a2 s.co  m*/
                public void onResult(final UpdateResult result, final Throwable t) {
                    onUpdateResultAction(result, t,
                            "successfully updated EntityObject field= " + field + " Entity = " + collectionName,
                            callback);
                }
            });
}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void updateDocumentFields(final String collectionName, Criteria criteria, HashMap<String, String> map,
        final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    List<Bson> list = new ArrayList<>();

    for (final Map.Entry m : map.entrySet()) {
        try {//ww  w .j a v a2 s .  c o  m
            Document doc = Document.parse(m.getValue().toString());
            list.add(set(m.getKey().toString(), doc));
        } catch (Exception e) {
            list.add(set(m.getKey().toString(), m.getValue().toString()));
        }
    }

    logger.info("updating EntityObject fields");
    collection.updateOne(criteria.getRestrictions(), combine(list), new SingleResultCallback<UpdateResult>() {
        @Override
        public void onResult(final UpdateResult result, final Throwable t) {
            onUpdateResultAction(result, t,
                    "successfully updated EntityObject fields Entity = " + collectionName, callback);
        }
    });

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void pushIntoDocumentField(final String collectionName, Criteria criteria, final String field,
        List<String> values, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    List<Document> docs = new ArrayList<>();
    List<String> strings = new ArrayList<>();

    for (String value : values) {
        try {/*  w  ww . ja v a 2  s  . c o  m*/
            docs.add(Document.parse(value));
        } catch (Exception e) {
            strings.add(value);
        }
    }

    if (!docs.isEmpty()) {

        collection.updateOne(criteria.getRestrictions(), addEachToSet(field, docs),
                new SingleResultCallback<UpdateResult>() {
                    @Override
                    public void onResult(final UpdateResult result, final Throwable t) {
                        onUpdateResultAction(result, t, "success", callback);
                    }
                });

    }

    if (!strings.isEmpty()) {

        collection.updateOne(criteria.getRestrictions(), addEachToSet(field, strings),
                new SingleResultCallback<UpdateResult>() {
                    @Override
                    public void onResult(final UpdateResult result, final Throwable t) {
                        onUpdateResultAction(result, t, "success", callback);
                    }
                });

    }

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void pullStringsFromDocumentField(final String collectionName, Criteria criteria, final String field,
        List<String> values, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.updateOne(criteria.getRestrictions(), pullAll(field, values),
            new SingleResultCallback<UpdateResult>() {
                @Override/*w  ww  .  ja va  2s .c  o m*/
                public void onResult(final UpdateResult result, final Throwable t) {
                    onUpdateResultAction(result, t, "success", callback);
                }
            });

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

void Recurse(final int index, final String entityName, final Criteria criteria, final String field,
        final String subField, final List<String> subFieldValues, final ResultCallback callback) {

    if (index == -1) {
        callback.onSuccess("");
        return;//from w w w  . j  av  a2  s. c o  m
    }

    MongoCollection<Document> collection = database.getCollection(entityName);

    collection.updateOne(criteria.getRestrictions(),
            pull(field, new Document(subField, subFieldValues.get(index))),
            new SingleResultCallback<UpdateResult>() {

                @Override
                public void onResult(final UpdateResult result, final Throwable t) {

                    if (t == null) {
                        if (result.getModifiedCount() > 0) {

                            Recurse(index - 1, entityName, criteria, field, subField, subFieldValues, callback);

                        } else {
                            callback.onError(errorMessage);
                        }
                    } else {
                        callback.onError(t.toString());
                        t.printStackTrace();
                    }

                }

            });

}

From source file:org.openo.commontosca.inventory.core.mongo.handler.model.MongoModelUpdateRequestHandler.java

License:Apache License

@Override
public SimpleDeferred<UpdateResult> handle(Update request) {
    DeferredResponse<UpdateResult> response = new DeferredResponse<>();
    try {/*from   ww w  .j  a  v  a2 s .  c om*/
        String modelName = request.getModelName();
        ValueMap model = request.getInventory().model().find().byName(request.getModelName()).execute().asOne();
        if (model == null || model.isEmpty()) {
            return response.reject("No such model: %s", modelName);
        }
        ValueMap valueMap = request.getValue();
        ValueMap strictMap = new ModelTypeVerifier(valueMap).policy(StrictPolicy.DEFINE_UPDATE_DATA).verify();
        verifierModelLabelUique(request, strictMap);
        if (strictMap.containsKey(ModelKey.NAME) && !modelName.equals(strictMap.optValue(ModelKey.NAME))) {
            return response.reject(
                    I18n.getLabel("model.name.can.not.update", strictMap.optValue(ModelKey.NAME), modelName),
                    modelName);
        }
        strictMap.put(ModelKey.LAST_MODIFIED, new Date());
        strictMap.remove(CommonKey.ID);
        strictMap.remove(ModelKey.NAME);
        boolean isEnable = false;
        if (strictMap.containsKey(ModelKey.ENABLE)) {
            isEnable = strictMap.optValue(ModelKey.ENABLE);
        } else {
            isEnable = model.optValue(ModelKey.ENABLE);
        }

        strictMap.put(ModelKey.ENABLE, false);
        MongoCollection<Document> modelCollection = this.getDatabase(request)
                .getCollection(Model.MODEL_DEFAULT_COLLECTION_NAME);
        com.mongodb.client.result.UpdateResult updateResult = DeferredSingleResult.<com.mongodb.client.result.UpdateResult>execute(
                deferred -> {
                    modelCollection.updateOne(new Document(ModelKey.NAME.getKeyName(), request.getModelName()),
                            new Document("$set", strictMap), deferred);
                });
        if (updateResult.getModifiedCount() > 0 && isEnable) {
            this.ensureDataCollection(request);
            DeferredSingleResult.<com.mongodb.client.result.UpdateResult>execute(deferred -> {
                modelCollection.updateOne(new Document(ModelKey.NAME.getKeyName(), request.getModelName()),
                        new Document("$set", Collections.singletonMap(ModelKey.ENABLE.getKeyName(), true)),
                        deferred);
            });
        }
        response.resolve(new DefaultUpdateResult(updateResult.getModifiedCount()));
    } catch (Exception e) {
        response.reject(e);
    }
    return response;
}