Example usage for com.mongodb DBObject put

List of usage examples for com.mongodb DBObject put

Introduction

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

Prototype

Object put(String key, Object v);

Source Link

Document

Sets a name/value pair in this object.

Usage

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

License:Apache License

public void shardCollection(ButtonBase button) {
    FormDialog dialog = (FormDialog) ((MenuItem) getBoundUnit(Item.shardCollection)).getDialog();
    ComboBox combo = (ComboBox) getBoundUnit(Item.shardKeyCombo);
    combo.value = 0;//  w  w w  .j  ava  2 s  . co m
    List<DBObject> indices = getCollectionNode().getCollection().getIndexInfo();
    String[] items = new String[indices.size() + 1];
    items[0] = "None";
    int i = 1;
    for (DBObject index : indices) {
        items[i++] = ((DBObject) index.get("key")).toString();
    }
    combo.items = items;
    combo.structureComponent();

    if (!dialog.show()) {
        return;
    }

    DBObject key = null;
    int index = combo.getComponentIntValue();
    if (index > 0) {
        key = (DBObject) indices.get(index - 1).get("key");
    } else {
        key = ((DocBuilderField) getBoundUnit(Item.shardCustomKey)).getDBObject();
    }

    if (key == null) {
        new InfoDialog(null, "Empty key", null, "You must select a shard key").show();
        return;
    }
    if (!new ConfirmDialog(null, "Confirm shard key", null,
            "About to shard collection with key " + key + ", is it correct? This operation cannot be undone.")
                    .show()) {
        return;
    }

    boolean unique = getBooleanFieldValue(Item.shardUniqueIndex);
    DB admin = getCollectionNode().getDbNode().getDb().getSisterDB("admin");
    DBObject cmd = new BasicDBObject("shardCollection", getCollectionNode().getCollection().getFullName());
    cmd.put("key", key);
    if (unique) {
        cmd.put("unique", unique);
    }
    new DbJobCmd(admin, cmd, this, null).addJob();

}

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

License:Apache License

private Object handleSpecialFields(DBObject doc) {
    for (String field : doc.keySet()) {
        if (field.equals("__rand")) {
            String type = (String) doc.get(field);
            if (type.equals("int")) {
                int min = (Integer) doc.get("min");
                int max = (Integer) doc.get("max");
                return min + (int) (Math.random() * ((max - min) + 1));
            } else if (type.equals("str")) {
                int len = (Integer) doc.get("len");
                StringBuilder sb = new StringBuilder(len);
                byte min = 0x61;
                byte max = 0x7a;
                for (int i = 0; i < len; ++i) {
                    char c = (char) (min + (byte) (Math.random() * ((max - min) + 1)));
                    sb.append(c);/*from   w w  w  .j  a v  a2s.com*/
                }
                return sb.toString();
            }
        }
        Object val = doc.get(field);
        if (val instanceof BasicDBObject) {
            BasicDBObject subdoc = (BasicDBObject) val;
            Object res = handleSpecialFields(subdoc);
            if (res != null) {
                doc.put(field, res);
            }
        } else if (val instanceof BasicDBList) {
            BasicDBList sublist = (BasicDBList) val;
            handleSpecialFields(sublist);
        }
    }
    return null;
}

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

License:Apache License

public void geoNear(ButtonBase button) {
    DBObject cmd = new BasicDBObject("geoNear", getCollectionNode().getCollection().getName());
    DBObject origin = ((DocBuilderField) getBoundUnit(Item.gnOrigin)).getDBObject();
    cmd.put("near", origin);
    double distance = getDoubleFieldValue(Item.gnMaxDistance);
    cmd.put("maxDistance", distance);
    int limit = getIntFieldValue(Item.gnLimit);
    cmd.put("limit", limit);
    double distanceMult = getDoubleFieldValue(Item.gnDistanceMultiplier);
    if (distanceMult > 0) {
        cmd.put("distanceMultiplier", distanceMult);
    }/*from  w  ww . j a va2s.  c  om*/
    DBObject query = ((DocBuilderField) getBoundUnit(Item.gnQuery)).getDBObject();
    if (query != null) {
        cmd.put("query", query);
    }
    boolean spherical = getBooleanFieldValue(Item.gnSpherical);
    if (spherical) {
        cmd.put("spherical", true);
    }
    DBObject search = ((DocBuilderField) getBoundUnit(Item.gnSearch)).getDBObject();
    if (search != null) {
        cmd.put("search", search);
    }

    new DbJobCmd(getCollectionNode().getDbNode().getDb(), cmd).addJob();
}

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

License:Apache License

public void fixCollection(ButtonBase button) {
    final MongoClient m = getCollectionNode().getDbNode().getMongoNode().getMongoClient();
    ArrayList<MongoNode> mongoNodes = UMongo.instance.getMongos();
    String[] mongonames = new String[mongoNodes.size() - 1];
    MongoClient[] mongos = new MongoClient[mongonames.length];
    int i = 0;//from w w w . ja  v  a2  s  .co  m
    for (MongoNode node : mongoNodes) {
        MongoClient m2 = node.getMongoClient();
        if (m == m2) {
            continue;
        }
        mongonames[i] = m2.toString();
        mongos[i] = m2;
        ++i;
    }
    ComboBox src = (ComboBox) getBoundUnit(Item.fcSrcMongo);
    src.items = mongonames;
    src.structureComponent();
    FormDialog dialog = (FormDialog) getBoundUnit(Item.fcDialog);
    if (!dialog.show()) {
        return;
    }

    final DBCollection dstCol = getCollectionNode().getCollection();
    final MongoClient srcMongo = mongos[src.getIntValue()];
    final boolean upsert = getBooleanFieldValue(Item.fcUpsert);

    final String dbname = dstCol.getDB().getName();
    final String colname = dstCol.getName();
    final DBCollection srcCol = srcMongo.getDB(dbname).getCollection(colname);
    String txt = "About to copy from ";
    txt += srcMongo.getConnectPoint() + "(" + srcCol.count() + ")";
    txt += " to ";
    txt += m.getConnectPoint() + "(" + dstCol.count() + ")";
    if (!new ConfirmDialog(null, "Confirm Fix Collection", null, txt).show()) {
        return;
    }

    new DbJob() {
        @Override
        public Object doRun() {
            DBCursor cur = srcCol.find();
            int count = 0;
            int dup = 0;
            while (cur.hasNext()) {
                DBObject obj = cur.next();
                if (upsert) {
                    BasicDBObject id = new BasicDBObject("_id", obj.get("_id"));
                    dstCol.update(id, obj, true, false);
                } else {
                    try {
                        dstCol.insert(obj);
                    } catch (DuplicateKey e) {
                        // dup keys are expected here
                        ++dup;
                    }
                }
                ++count;
            }
            DBObject res = new BasicDBObject("count", count);
            res.put("dups", dup);
            return res;
        }

        @Override
        public String getNS() {
            return "*";
        }

        @Override
        public String getShortName() {
            return "Fix Collection";
        }
    }.addJob();
}

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.ja v  a  2  s  .c om
    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 command(final ButtonBase button) {
    final DB db = getDbNode().getDb();
    final String scmd = getStringFieldValue(Item.commandStr);
    final DBObject cmd = !scmd.isEmpty() ? new BasicDBObject(scmd, 1)
            : ((DocBuilderField) getBoundUnit(Item.commandJson)).getDBObject();
    boolean help = getBooleanFieldValue(Item.commandHelp);
    if (help) {/*from  w  w  w  .  j  ava  2 s .  c o m*/
        cmd.put("help", true);
    }

    new DbJobCmd(db, cmd, null, button).addJob();
}

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  ww.  ja  v a2s.co 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.edgytech.umongo.DbPanel.java

License:Apache License

public void profile(ButtonBase button) {
    DB db = getDbNode().getDb();/* w ww .  j a v  a 2s .  c om*/
    int level = getIntFieldValue(Item.profileLevel);
    final DBObject cmd = new BasicDBObject("profile", level);
    if (level == 1) {
        int slow = getIntFieldValue(Item.profileSlowMS);
        if (slow > 0) {
            cmd.put("slowms", slow);
        }
    }
    new DbJobCmd(db, cmd).addJob();
}

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

License:Apache License

public void movePrimary(ButtonBase button) {
    FormDialog dialog = (FormDialog) ((MenuItem) getBoundUnit(Item.movePrimary)).getDialog();
    ComboBox combo = (ComboBox) getBoundUnit(Item.mvpToShard);
    combo.value = 0;//from  ww  w.j a  v  a  2  s. c  om
    combo.items = getDbNode().getMongoNode().getShardNames();
    combo.structureComponent();

    if (!dialog.show()) {
        return;
    }

    MongoClient m = getDbNode().getMongoNode().getMongoClient();
    DB admin = m.getDB("admin");
    String shard = getStringFieldValue(Item.mvpToShard);
    DBObject cmd = new BasicDBObject("movePrimary", getDbNode().getDb().getName());
    cmd.put("to", shard);
    new DbJobCmd(admin, cmd, this, null).addJob();
}

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

License:Apache License

public void findJSFunction(ButtonBase button) {
    final DB db = getDbNode().getDb();
    final DBCollection col = db.getCollection("system.js");
    final String name = getStringFieldValue(Item.findJSFunctionName);
    DBObject query = new BasicDBObject();
    if (name != null && !name.isEmpty()) {
        query.put("_id", name);
    }/*w  w  w  .j  a v  a 2 s  .  c o m*/
    CollectionPanel.doFind(col, query);
}