Example usage for com.mongodb DBObject containsField

List of usage examples for com.mongodb DBObject containsField

Introduction

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

Prototype

boolean containsField(String s);

Source Link

Document

Checks if this object contains a field with the given name.

Usage

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

License:Apache License

public void setValue(ButtonBase button) {
    final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit());
    if (dv.getDBCursor() == null) {
        // local data
        new InfoDialog(null, null, null, "Cannot do in-place update on local data.").show();
        return;//from w w  w  . j  ava 2  s  .c o m
    }

    TreeNodeDocumentField field = (TreeNodeDocumentField) dv.getSelectedNode().getUserObject();
    DBObject doc = dv.getSelectedDocument();
    String path = dv.getSelectedDocumentPath();
    Object newValue = UMongo.instance.getGlobalStore().editValue(field.getKey(), field.getValue());

    if (newValue == null) {
        //            new InfoDialog(null, null, null, "Cannot edit this type of data.").show();
        return;
    }

    final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc;
    DBObject setValue = new BasicDBObject(path, newValue);
    final DBObject update = new BasicDBObject("$set", setValue);

    final DBCollection col = dv.getDBCursor().getCollection();
    new DbJob() {

        @Override
        public Object doRun() {
            return col.update(query, update, false, false);
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Update";
        }

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject obj = new BasicDBObject("query", query);
            obj.put("update", update);
            return obj;
        }

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            dv.refresh(null);
        }
    }.addJob();
}

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

License:Apache License

public void unsetValue(ButtonBase button) {
    final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit());
    DBObject doc = dv.getSelectedDocument();
    String path = dv.getSelectedDocumentPath();

    if (dv.getDBCursor() == null) {
        // local data
        new InfoDialog(null, null, null, "Cannot do in-place update on local data.").show();
        return;/*  w w  w  .  ja  v a2  s.co m*/
    }

    if (!UMongo.instance.getGlobalStore().confirmDataLossOperation())
        return;

    final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc;
    DBObject setValue = new BasicDBObject(path, 1);
    final DBObject update = new BasicDBObject("$unset", setValue);

    final DBCollection col = dv.getDBCursor().getCollection();
    new DbJob() {

        @Override
        public Object doRun() {
            return col.update(query, update, false, false);
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Update";
        }

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject obj = new BasicDBObject("query", query);
            obj.put("update", update);
            return obj;
        }

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            dv.refresh(null);
        }
    }.addJob();
}

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

License:Apache License

public void update(ButtonBase button) {
    final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit());
    TreeNodeDocument node = (TreeNodeDocument) dv.getSelectedNode().getUserObject();
    final DBObject doc = node.getDBObject();

    ((DocBuilderField) getBoundUnit(Item.upUpdate)).setDBObject((BasicDBObject) doc);
    if (!((MenuItem) getBoundUnit(Item.update)).getDialog().show()) {
        return;/*from  w w w . j  av  a 2s.  c  o  m*/
    }

    final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc;
    final DBObject update = ((DocBuilderField) getBoundUnit(Item.upUpdate)).getDBObject();

    if (dv.getDBCursor() == null) {
        // local data
        Tree tree = dv.getTree();
        tree.removeChild(node);
        dv.addDocument(update, null);
        tree.structureComponent();
        tree.expandNode(tree.getTreeNode());
        return;
    }

    final DBCollection col = dv.getDBCursor().getCollection();
    new DbJob() {

        @Override
        public Object doRun() {
            return col.update(query, update, false, false);
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Update";
        }

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject obj = new BasicDBObject("query", query);
            obj.put("update", update);
            return obj;
        }

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            dv.refresh(null);
        }
    }.addJob();
}

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

License:Apache License

public void remove(ButtonBase button) {
    final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit());
    TreeNodeDocument node = (TreeNodeDocument) dv.getSelectedNode().getUserObject();
    final DBObject doc = node.getDBObject();

    if (dv.getDBCursor() == null) {
        // local data
        Tree tree = dv.getTree();// w  ww . jav  a 2  s . c o m
        tree.removeChild(node);
        tree.structureComponent();
        tree.expandNode(tree.getTreeNode());
        return;
    }

    // go by _id if possible
    final DBObject query = doc.containsField("_id") ? new BasicDBObject("_id", doc.get("_id")) : doc;
    final DBCollection col = dv.getDBCursor().getCollection();
    new DbJob() {

        @Override
        public Object doRun() {
            return col.remove(query);
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Remove";
        }

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            dv.refresh(null);
        }

        @Override
        public DBObject getRoot(Object result) {
            return query;
        }
    }.addJob();
}

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

License:Apache License

private Object getFieldValueRecursive(DBObject obj, String field) {
    if (field.indexOf(".") < 0)
        return obj.get(field);

    String[] tokens = field.split("\\.");
    for (int i = 0; i < tokens.length - 1; ++i) {
        String f = tokens[i];/*from  www.java2  s  .  co  m*/
        if (!obj.containsField(f))
            return null;
        Object o = obj.get(f);
        if (!DBObject.class.isInstance(o))
            return null;
        obj = (DBObject) o;
    }
    return obj.get(tokens[tokens.length - 1]);
}

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

License:Apache License

public static DBObject getReplicaSetInfo(MongoClient mongo) {
    DB db = mongo.getDB("local");
    DBObject result = new BasicDBObject();
    DBCollection namespaces = db.getCollection("system.namespaces");
    String oplogName;//from   ww w.  j  a v  a  2  s  . c  o m
    if (namespaces.findOne(new BasicDBObject("name", "local.oplog.rs")) != null) {
        oplogName = "oplog.rs";
    } else if (namespaces.findOne(new BasicDBObject("name", "local.oplog.$main")) != null) {
        oplogName = "oplog.$main";
    } else {
        return null;
    }
    DBObject olEntry = namespaces.findOne(new BasicDBObject("name", "local." + oplogName));
    if (olEntry != null && olEntry.containsField("options")) {
        BasicDBObject options = (BasicDBObject) olEntry.get("options");
        long size = options.getLong("size");
        result.put("logSizeMB", Float.valueOf(String.format("%.2f", size / 1048576f)));
    } else {
        return null;
    }
    DBCollection oplog = db.getCollection(oplogName);
    int size = oplog.getStats().getInt("size");
    result.put("usedMB", Float.valueOf(String.format("%.2f", size / 1048576f)));

    DBCursor firstc = oplog.find().sort(new BasicDBObject("$natural", 1)).limit(1);
    DBCursor lastc = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1);
    if (!firstc.hasNext() || !lastc.hasNext()) {
        return null;
    }
    BasicDBObject first = (BasicDBObject) firstc.next();
    BasicDBObject last = (BasicDBObject) lastc.next();
    BSONTimestamp tsfirst = (BSONTimestamp) first.get("ts");
    BSONTimestamp tslast = (BSONTimestamp) last.get("ts");
    if (tsfirst == null || tslast == null) {
        return null;
    }

    int ftime = tsfirst.getTime();
    int ltime = tslast.getTime();
    int timeDiffSec = ltime - ftime;
    result.put("timeDiff", timeDiffSec);
    result.put("timeDiffHours", Float.valueOf(String.format("%.2f", timeDiffSec / 3600f)));
    result.put("tFirst", new Date(ftime * 1000l));
    result.put("tLast", new Date(ltime * 1000l));
    result.put("now", new Date());
    return result;
}

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

License:Apache License

void update(int options, WriteConcern wc, ReadPreference rp) {
    // reset/*from www.  j  ava  2  s  . co m*/
    xmlLoadCheckpoint();

    setBooleanFieldValue(Item.tailable, (options & Bytes.QUERYOPTION_TAILABLE) != 0);
    setBooleanFieldValue(Item.slaveOk, (options & Bytes.QUERYOPTION_SLAVEOK) != 0);
    setBooleanFieldValue(Item.opLogReplay, (options & Bytes.QUERYOPTION_OPLOGREPLAY) != 0);
    setBooleanFieldValue(Item.noTimeout, (options & Bytes.QUERYOPTION_NOTIMEOUT) != 0);
    setBooleanFieldValue(Item.awaitData, (options & Bytes.QUERYOPTION_AWAITDATA) != 0);
    setBooleanFieldValue(Item.exhaust, (options & Bytes.QUERYOPTION_EXHAUST) != 0);
    setBooleanFieldValue(Item.partial, (options & Bytes.QUERYOPTION_PARTIAL) != 0);

    Object w = wc.getWObject();
    int wInt = (Integer) (w instanceof Integer ? w : 0);
    String wStr = (String) (w instanceof String ? w : "");
    setIntFieldValue(Item.writeFactor, wInt);
    setStringFieldValue(Item.writePolicy, wStr);
    setIntFieldValue(Item.writeTimeout, wc.getWtimeout());
    //        setBooleanFieldValue(Item.fsync, wc.fsync());

    DBObject rpObj = rp.toDBObject();
    ComboBox readBox = (ComboBox) getBoundUnit(Item.rpPreference);
    ReadPref rpEnm = ReadPref.primary;
    if (rp != null)
        rpEnm = ReadPref.valueOf(rp.getName());
    readBox.value = rpEnm.ordinal();
    if (rpObj.containsField("tags")) {
        List tags = (List) rpObj.get("tags");
        if (tags.size() > 0) {
            ((DocBuilderField) getBoundComponentUnit(Item.rpTag)).setDBObject((DBObject) tags.get(0));
        }
    }
}

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

License:Apache License

void refreshTagList() {
    String shardName = getReplSetNode().getShardName();
    if (shardName == null)
        return;/*w  w w.jav  a 2  s .c om*/

    ListArea list = (ListArea) getBoundUnit(Item.tagList);
    final DB db = ((RouterNode) getReplSetNode().getParentNode()).getMongoClient().getDB("config");
    DBObject shard = db.getCollection("shards").findOne(new BasicDBObject("_id", shardName));
    if (shard.containsField("tags")) {
        BasicDBList tags = (BasicDBList) shard.get("tags");
        if (tags.size() > 0) {
            String[] array = new String[tags.size()];
            int i = 0;
            for (Object tag : tags) {
                array[i++] = (String) tag;
            }
            list.items = array;
            list.structureComponent();
            return;
        }
    }
    list.items = null;
    list.structureComponent();
}

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

License:Apache License

static String[] getExistingTags(DB config) {
    DBCursor cur = config.getCollection("shards").find();
    HashSet<String> tags = new HashSet<String>();
    while (cur.hasNext()) {
        DBObject shard = cur.next();
        if (shard.containsField("tags")) {
            BasicDBList list = (BasicDBList) shard.get("tags");
            for (Object tag : list) {
                tags.add((String) tag);
            }/*ww  w.ja  va  2 s  .co  m*/
        }
    }
    return tags.toArray(new String[tags.size()]);
}

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

License:Apache License

@Override
public <T, R> DBObject translateProjection(Query<T, R> query) {
    if (query.getReturnFields().size() > 0) {
        DBObject fields = object();
        for (String field : query.getReturnFields()) {
            fields.put(getPropertyResolver().resolve(field, query.getEntityClass()), 1);
        }/*  w  w  w  . j  a va2 s  .  com*/
        // if _id is not present then exclude it
        if (!fields.containsField(Mapper.ID_KEY)) {
            fields.put(Mapper.ID_KEY, 0);
        }
        return fields;
    } else {
        return null;
    }
}