Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find() 

Source Link

Document

Select all documents in collection and get a cursor to the selected documents.

Usage

From source file:com.ebay.cloud.cms.config.CMSProperties.java

License:Apache License

private void loadProperties(Mongo mongo) {
    Map<String, Object> m = new ConcurrentHashMap<String, Object>();
    DBCollection collection = getPropertiesCollection(mongo);
    collection.setReadPreference(ReadPreference.primary());
    DBCursor cursor = collection.find();
    while (cursor.hasNext()) {
        BasicDBObject object = (BasicDBObject) cursor.next();
        String key = getKey(object);
        if (key != null) {
            m.put(key, object.get(key));
        }/*from  ww  w  .  java 2 s.  co m*/
    }

    cachedConfigurations = m;
}

From source file:com.ebay.cloud.cms.config.CMSProperties.java

License:Apache License

public void updateConfig(Map<String, Object> configs) {
    DBCollection coll = getPropertiesCollection(ds.getMongoInstance());
    DBCursor cursor = coll.find();
    // update existing
    while (cursor.hasNext()) {
        BasicDBObject dbo = (BasicDBObject) cursor.next();
        String key = getKey(dbo);
        if (!configs.containsKey(key)) {
            continue;
        }//  ww  w. j  a  v a2 s  .  c o  m

        BasicDBObject qObject = new BasicDBObject();
        BasicDBObject vObject = new BasicDBObject();
        qObject.append("_id", dbo.get("_id"));
        vObject.append(key, configs.get(key));

        coll.update(qObject, vObject);
        configs.remove(key);
    }

    // insert new config
    if (!configs.isEmpty()) {
        List<DBObject> list = new ArrayList<DBObject>();
        for (Entry<String, Object> entry : configs.entrySet()) {
            DBObject dbo = new BasicDBObject();
            dbo.put(entry.getKey(), entry.getValue());

            list.add(dbo);
        }
        coll.insert(list);
    }

    loadProperties(ds.getMongoInstance());
}

From source file:com.ebay.jetstream.config.mongo.MongoDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationDo> findConfigurationByAppNameAndVersion(BasicDBObject query,
        MongoConnection mongoConnection) {

    List<JetStreamBeanConfigurationDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }//from   w  ww. j ava  2s.  co m

    Exception e = null;
    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.config.mongo.MongoDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationDo> findConfigurationByQuery(BasicDBObject query,
        MongoConnection mongoConnection) {

    List<JetStreamBeanConfigurationDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }//  w  w w .  ja v  a 2 s  . c o m

    Exception e = null;

    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
            //beanConfig = (JetStreamBeanConfigurationDo)fromJson(jsonString, JetStreamBeanConfigurationDo.class);
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.configurationmanagement.MongoLogDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationLogDo> findConfigurationByAppNameAndVersion(BasicDBObject query,
        MongoLogConnection mongoConnection) {

    List<JetStreamBeanConfigurationLogDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationLogDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfiglog collection is unknown");
    }//  w  w  w.java  2s. c  om

    Exception e = null;
    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.configurationmanagement.MongoLogDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationLogDo> findConfigurationByQuery(BasicDBObject query,
        MongoLogConnection mongoLogConnection) {

    List<JetStreamBeanConfigurationLogDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationLogDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoLogConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfigLog collection is unknown");
    }/*w w  w  .j a va 2 s.  c om*/

    Exception e = null;

    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
            // beanConfig =
            // (JetStreamBeanConfigurationDo)fromJson(jsonString,
            // JetStreamBeanConfigurationDo.class);
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.edduarte.argus.Context.java

License:Apache License

/**
 * Collects the existing differences that were stored in the database.
 *///from w  w w .j  av  a  2s.  c om
@Override
public List<Difference> getExistingDifferences(String url) {
    DBCollection diffColl = differencesDB.getCollection(url);
    Iterable<DBObject> cursor = diffColl.find();
    return StreamSupport.stream(cursor.spliterator(), true).map(Difference::new).collect(Collectors.toList());
}

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  ww .j  a va 2s .  c o  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.DbPanel.java

License:Apache License

public void listFiles(ButtonBase button) {
    final DB db = getDbNode().getDb();
    final DBCollection col = db.getCollection("fs.files");

    new DbJob() {

        @Override/*ww  w.java2s .c om*/
        public Object doRun() throws IOException {
            return col.find();
        }

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

        @Override
        public String getShortName() {
            return "List Files";
        }
    }.addJob();
}

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;// www. j  a va  2s  .  c om
    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;
}