Example usage for com.mongodb DBCollection update

List of usage examples for com.mongodb DBCollection update

Introduction

In this page you can find the example usage for com.mongodb DBCollection update.

Prototype

public WriteResult update(final DBObject query, final DBObject update, final boolean upsert,
        final boolean multi, final WriteConcern concern, @Nullable final DBEncoder encoder) 

Source Link

Document

Modify an existing document or documents in collection.

Usage

From source file:ezbake.data.mongo.MongoDriverHandler.java

License:Apache License

public EzWriteResult update_driver(String collection, EzUpdateRequest req, EzSecurityToken token)
        throws TException, EzMongoDriverException {
    if (!collection.equals("fs.chunks") && !collection.equals("fs.files")) {
        collection = appName + "_" + collection;
    }//from  ww  w  . ja  va2 s  .  c  o  m

    appLog.debug("update_driver() collection: {}", collection);

    TokenUtils.validateSecurityToken(token, handler.getConfigurationProperties());
    EzWriteResult ewr = new EzWriteResult();

    try {
        DBCollection c = handler.db.getCollection(normalizeCollection(collection));

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(req.getQuery()));
        DBObject query = (DBObject) ois.readObject();

        appLog.debug("QUERY {}", query);

        ois = new ObjectInputStream(new ByteArrayInputStream(req.getDbUpdateObject()));
        DBObject updateObject = (DBObject) ois.readObject();

        appLog.debug("UPDATE OBJECT {}", updateObject);

        ois = new ObjectInputStream(new ByteArrayInputStream(req.getWriteConcern()));
        WriteConcern concern = (WriteConcern) ois.readObject();

        ois = new ObjectInputStream(new ByteArrayInputStream(req.getDbEncoder()));
        DBEncoder encoder = (DBEncoder) ois.readObject();

        Boolean isDriverUnitTestMode = req.isIsUnitTestMode();

        HashMap<String, String> auditParamsMap = new HashMap<>();
        auditParamsMap.put("action", "update_driver");
        auditParamsMap.put("collectionName", collection);
        auditParamsMap.put("query", handler.printMongoObject(query));
        auditParamsMap.put("updateObject", handler.printMongoObject(updateObject));
        auditParamsMap.put("concern", handler.printMongoObject(concern));
        auditParamsMap.put("encoder", handler.printMongoObject(encoder));
        handler.auditLog(token, AuditEventType.FileObjectModify, auditParamsMap);

        // assume it's a WRITE operation - then we need to iterate through the redacted results
        //   and see if they are updating the security fields - then it becomes MANAGE operation.
        QueryResultIterator qri = findHandler.convertFindForDriver(normalizeCollection(collection), query, null,
                "", 0, 0, 0, null, token, EzMongoHandler.WRITE_OPERATION);

        final List<Object> idList = new ArrayList<Object>();
        while (qri.hasNext()) {
            DBObject o = qri.next();
            if (!isDriverUnitTestMode) {
                // also need to check if the user has the auths to update the record
                try {
                    boolean isManageOperation = handler.getMongoUpdateHelper().isUpdatingSecurityFields(o,
                            updateObject);
                    handler.getMongoInsertHelper().checkAbilityToInsert(token, null, updateObject, o,
                            isManageOperation, true);
                    appLog.debug("can update DBObject (_id): {}", o.get("_id"));
                    idList.add(o.get("_id"));
                } catch (ClassCastException | VisibilityParseException | EzMongoBaseException e) {
                    appLog.error(e.toString());
                    appLog.debug("User does not have the auths to update record: {}", o);
                }
            } else {
                appLog.debug("can update DBObject (_id): {}", o.get("_id"));
                idList.add(o.get("_id"));
            }
        }

        final DBObject inClause = new BasicDBObject("$in", idList);
        final DBObject redactedQuery = new BasicDBObject("_id", inClause);

        WriteResult res = null;
        // only update the objects that were returned after performing the redact
        if (idList.size() > 0) {
            res = c.update(redactedQuery, updateObject, req.isUpsert(), req.isMulti(), concern, encoder);
        } else {
            throw new MongoException("Nothing to update, perhaps redact prohibited. "
                    + "Also note that upsert is not supported. If you used save() to make this call,"
                    + " try using insert() instead is possible");
        }

        appLog.debug("WriteResult: {}", res);

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        new ObjectOutputStream(bOut).writeObject(res);

        ewr.setWriteResult(bOut.toByteArray());
    } catch (MongoException e) {
        appLog.error(e.toString());
        addWriteResultException(ewr, e);
    } catch (Exception e) {
        appLog.error(e.toString());
        throw new TException(e);
    }
    return ewr;
}