Example usage for com.mongodb DBObject putAll

List of usage examples for com.mongodb DBObject putAll

Introduction

In this page you can find the example usage for com.mongodb DBObject putAll.

Prototype

void putAll(BSONObject o);

Source Link

Document

Sets all key/value pairs from an object into this object

Usage

From source file:br.ufabc.impress.mongo.manager.DBHelper.java

@Override
public List<Boolean> modifyNotOveride(String tableName, String json, String _id) {
    if (tableName == null || tableName.equals("") || json == null || json.equals("") || _id == null
            || _id.equals("")) {
        return null;
    }//from   w  w  w. j a  v a2s .c o m
    List<Boolean> resList = new ArrayList();
    DBCollection table = db.getCollection(tableName);
    DBCursor cursor = table.find();
    while (cursor.hasNext()) {
        DBObject updateDocument = cursor.next();
        DBObject searchQuery = cursor.next();
        DBObject dbObject = (DBObject) JSON.parse(json);
        updateDocument.putAll(dbObject.toMap());
        updateDocument.removeField("_id");
        WriteResult result = table.update(searchQuery, updateDocument);
        resList.add(result.isUpdateOfExisting());
    }
    return resList;
}

From source file:com.edgytech.umongo.CreateIndexDialog.java

License:Apache License

DBObject getOptions() {
    final DBObject opts = new BasicDBObject();
    final String name = getStringFieldValue(Item.name);
    if (name != null && !name.trim().isEmpty()) {
        opts.put("name", name);
    }//from w  w  w. j a v a 2 s  .  co  m
    if (getBooleanFieldValue(Item.unique)) {
        opts.put("unique", true);
    }
    if (getBooleanFieldValue(Item.dropDuplicates)) {
        opts.put("dropDups", true);
    }
    if (getBooleanFieldValue(Item.sparse)) {
        opts.put("sparse", true);
    }
    if (getBooleanFieldValue(Item.expireDocuments)) {
        opts.put("expireAfterSeconds", getIntFieldValue(Item.expireAfterSeconds));
    }
    if (getBooleanFieldValue(Item.background)) {
        opts.put("background", true);
    }

    DBObject weights = ((DocBuilderField) getBoundUnit(Item.weights)).getDBObject();
    if (weights != null) {
        opts.put("weights", weights);
    }
    String defaultLanguage = getStringFieldValue(Item.defaultLanguage);
    if (!defaultLanguage.trim().isEmpty()) {
        opts.put("default_language", defaultLanguage);
    }
    String languageOverride = getStringFieldValue(Item.languageOverride);
    if (!languageOverride.trim().isEmpty()) {
        opts.put("language_override", languageOverride);
    }

    DBObject extra = ((DocBuilderField) getBoundUnit(Item.extra)).getDBObject();
    if (extra != null) {
        opts.putAll(extra);
    }
    return opts;
}

From source file:com.edgytech.umongo.DbPanel.java

License:Apache License

public void createCollection(final ButtonBase button) {
    final DbNode node = getDbNode();
    final DB db = getDbNode().getDb();
    final String name = getStringFieldValue(Item.createCollName);
    final boolean capped = getBooleanFieldValue(Item.createCollCapped);
    final int size = getIntFieldValue(Item.createCollSize);
    final int count = getIntFieldValue(Item.createCollCount);
    //        final boolean autoIndexId = getBooleanFieldValue(Item.createCollAutoIndex);
    final boolean usePowerOf2Sizes = getBooleanFieldValue(Item.createCollUsePowerOf2Sizes);

    DBObject createCmd = new BasicDBObject("create", name);
    DBObject opt = new BasicDBObject("capped", capped);
    if (capped) {
        if (size > 0) {
            opt.put("size", size);
        }/*from w w w  .  j  a v  a 2  s.c o  m*/
        if (count > 0) {
            opt.put("max", count);
        }
    }

    //                // deprecated
    //                if (!autoIndexId) {
    //                    opt.put("autoIndexId", false);
    //                }
    // usePowerOf2Sizes uses flags name :(
    opt.put("flags", usePowerOf2Sizes ? 1 : 0);

    createCmd.putAll(opt);
    new DbJobCmd(db, createCmd, null, node, button).addJob();
}

From source file:com.eharmony.matching.seeking.translator.mongodb.MongoQueryTranslator.java

License:Apache License

@Override
public DBObject order(DBObject... orders) {
    DBObject merged = object();
    for (DBObject order : orders) {
        merged.putAll(order);
    }//from  w  w w . j av a 2  s . c  o m
    return merged;
}

From source file:com.kurniakue.trxreader.data.TransactionD.java

public void fixAmount(String dateStr) {
    DBCursor cursor;/*from   w w w  .  j a va2 s.  c  o  m*/
    if (dateStr == null || "".equals(dateStr)) {
        cursor = transactions.find();
    } else {
        cursor = transactions.find(new BasicDBObject(F.Date.name(), dateStr));
    }
    while (cursor.hasNext()) {
        DBObject query = cursor.next();
        DBObject update = new BasicDBObject();
        update.putAll(query);
        update.put(F.Amount.name(),
                Tool.tint(query.get(F.Price.name())) * Tool.tint(query.get(F.Count.name())));
        transactions.update(query, update);
    }
}

From source file:com.kurniakue.trxreader.data.TransactionD.java

public void addCreditFlag(String dateStr) {
    DBCursor cursor;//from w  w w  .  jav a  2 s.  com
    if (dateStr == null || "".equals(dateStr)) {
        cursor = transactions.find();
    } else {
        cursor = transactions.find(new BasicDBObject(F.Date.name(), dateStr));
    }
    while (cursor.hasNext()) {
        DBObject query = cursor.next();
        DBObject update = new BasicDBObject();
        update.putAll(query);
        int dcflag = -1;
        if (CASH.equals(update.get(F.ItemNo.name()))) {
            dcflag = 1;
        }
        update.put(F.DCFlag.name(), dcflag);
        transactions.update(query, update);
    }
}

From source file:com.kurniakue.trxreader.data.TransactionD.java

public void fixRkapField(String dateStr) {
    DBCursor cursor;//from w  ww. j a v  a 2  s  .c  o  m
    if (dateStr == null || "".equals(dateStr)) {
        cursor = transactions.find();
    } else {
        cursor = transactions.find(new BasicDBObject(F.Date.name(), dateStr));
    }
    while (cursor.hasNext()) {
        DBObject query = cursor.next();
        DBObject update = new BasicDBObject();
        update.putAll(query);
        Record record = Record.r(update);
        if (!RKAP.equals(record.get(F.ItemNo))) {
            continue;
        }
        if (!record.getString(F.ItemName).startsWith("Saldo")) {
            continue;
        }

        if (record.getInt(F.Amount) < 0) {
            update.put(F.Amount.name(), record.getInt(F.Amount) * -1);
        }
        if (record.getInt(F.DCFlag) < 0) {
            update.put(F.DCFlag.name(), record.getInt(F.DCFlag) * -1);
        }

        // uncomment to actually update DB
        // and comment it back to prevent accidental update DB
        // transactions.update(query, update);
    }
}

From source file:com.socialsky.mods.MongoPersistor.java

License:Apache License

private DBObject sortObjectToDBObject(Object sortObj) {
    if (sortObj instanceof JsonObject) {
        // Backwards compatability and a simpler syntax for single-property sorting
        return jsonToDBObject((JsonObject) sortObj);
    } else if (sortObj instanceof JsonArray) {
        JsonArray sortJsonObjects = (JsonArray) sortObj;
        DBObject sortDBObject = new BasicDBObject();
        for (Object curSortObj : sortJsonObjects) {
            if (!(curSortObj instanceof JsonObject)) {
                throw new IllegalArgumentException(
                        "Cannot handle type " + curSortObj.getClass().getSimpleName());
            }//from   ww  w  .  j  av a 2 s  . co  m

            sortDBObject.putAll(((JsonObject) curSortObj).toMap());
        }

        return sortDBObject;
    } else {
        throw new IllegalArgumentException("Cannot handle type " + sortObj.getClass().getSimpleName());
    }
}

From source file:com.trenako.images.ImagesRepositoryImpl.java

License:Apache License

private DBObject fillMetadata(Map<String, String> metadata) {
    DBObject dbo = new BasicDBObject();
    dbo.putAll(metadata);
    return dbo;/*from   ww  w.  ja va 2  s .c  om*/
}

From source file:com.zjy.mongo.output.MongoRecordWriter.java

License:Apache License

@Override
public void write(final K key, final V value) throws IOException {
    if (value instanceof MongoUpdateWritable) {
        outputStream.writeInt(MongoWritableTypes.MONGO_UPDATE_WRITABLE);
        ((MongoUpdateWritable) value).write(outputStream);
    } else {//from  www  .  ja v  a2s. c  o  m
        DBObject o = new BasicDBObject();
        if (key instanceof BSONWritable) {
            o.put("_id", ((BSONWritable) key).getDoc());
        } else if (key instanceof BSONObject) {
            o.put("_id", key);
        } else {
            o.put("_id", BSONWritable.toBSON(key));
        }

        if (value instanceof BSONWritable) {
            o.putAll(((BSONWritable) value).getDoc());
        } else if (value instanceof MongoOutput) {
            ((MongoOutput) value).appendAsValue(o);
        } else if (value instanceof BSONObject) {
            o.putAll((BSONObject) value);
        } else {
            o.put("value", BSONWritable.toBSON(value));
        }
        outputStream.writeInt(MongoWritableTypes.BSON_WRITABLE);
        bsonWritable.setDoc(o);
        bsonWritable.write(outputStream);
    }
}