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.edgytech.umongo.RouterPanel.java

License:Apache License

public void regenConfigDB(ButtonBase button) throws UnknownHostException {
    MongoClient cmongo = getRouterNode().getMongoClient();
    String servers = getStringFieldValue(Item.regenServers);
    final String db = getStringFieldValue(Item.regenDB);
    final String col = getStringFieldValue(Item.regenCollection);
    final String ns = db + "." + col;
    final DBObject shardKey = ((DocBuilderField) getBoundUnit(Item.regenShardKey)).getDBObject();
    final boolean unique = getBooleanFieldValue(Item.regenKeyUnique);
    final BasicDBObject result = new BasicDBObject();
    result.put("ns", ns);
    result.put("shardKey", shardKey);
    result.put("unique", unique);

    // create direct mongo for each replica set
    String[] serverList = servers.split("\n");
    List<ServerAddress> list = new ArrayList<ServerAddress>();
    String txt = "";
    String primaryShard = null;// w w w. j ava 2s .c  om
    final BasicDBObject shardList = new BasicDBObject();
    HashMap<MongoClient, String> mongoToShard = new HashMap<MongoClient, String>();
    sLoop: for (String server : serverList) {
        server = server.trim();
        String[] tokens = server.split("/");
        if (tokens.length != 2) {
            new InfoDialog(null, "Error", null, "Server format must be like 'hostname:port/shard', one by line")
                    .show();
            return;
        }
        server = tokens[0];
        String shard = tokens[1];
        if (primaryShard == null) {
            primaryShard = shard;
        }
        ServerAddress addr = new ServerAddress(server);

        // filter out if replset already exists
        for (MongoClient replset : mongoToShard.keySet()) {
            if (replset.getServerAddressList().contains(addr)) {
                continue sLoop;
            }
        }

        list.clear();
        list.add(addr);
        MongoClient mongo = new MongoClient(list);
        //            UMongo.instance.addMongoClient(mongo, null);
        // make request to force server detection
        mongo.getDatabaseNames();
        mongoToShard.put(mongo, shard);

        String desc = null;
        if (!mongo.getDatabaseNames().contains(db) || !mongo.getDB(db).getCollectionNames().contains(col)) {
            desc = "Collection not present!";
        } else {
            // try to see if shard key has index
            DBObject index = mongo.getDB(db).getCollection("system.indexes")
                    .findOne(new BasicDBObject("key", shardKey));
            if (index != null) {
                desc = "shard key found";
            } else {
                desc = "shard key NOT found!";
            }
        }
        txt += mongo.toString() + " shard=" + shard + " - " + desc + "\n";
        BasicDBObject shardObj = new BasicDBObject("servers", mongo.toString());
        shardObj.put("status", desc);
        if (shardList.containsField(shard)) {
            new InfoDialog(null, "Error", null, "Duplicate Shard name " + shard).show();
            return;
        }
        shardList.put(shard, shardObj);
    }
    result.put("shards", shardList);

    FormDialog dia = (FormDialog) getBoundUnit(Item.regenRSList);
    dia.setStringFieldValue(Item.regenRSListArea, txt);
    if (!dia.show()) {
        return;
    }

    DB config = cmongo.getDB("config");

    // add database record
    BasicDBObject doc = new BasicDBObject("_id", db);
    doc.put("partitioned", true);
    doc.put("primary", primaryShard);
    config.getCollection("databases").save(doc);

    // add collection record
    doc = new BasicDBObject("_id", ns);
    doc.put("lastmod", new Date());
    doc.put("dropped", false);
    doc.put("key", shardKey);
    doc.put("unique", unique);
    config.getCollection("collections").save(doc);

    final DBCollection chunks = config.getCollection("chunks");
    long count = chunks.count(new BasicDBObject("ns", ns));
    if (count > 0) {
        dia = (FormDialog) getBoundUnit(Item.regenDeleteChunks);
        if (dia.show()) {
            chunks.remove(new BasicDBObject("ns", ns));
        } else {
            return;
        }
    }

    // add temp collection to sort chunks with shard key
    final DBCollection tmpchunks = config.getCollection("_tmpchunks_" + col);
    tmpchunks.drop();
    // should be safe environment, and dup keys should be ignored
    tmpchunks.setWriteConcern(WriteConcern.NORMAL);
    // can use shardKey as unique _id
    //        tmpchunks.ensureIndex(shardKey, "shardKey", true);

    // create filter for shard fields
    final DBObject shardKeyFilter = new BasicDBObject();
    //        final DBObject shardKeyDescend = new BasicDBObject();
    boolean hasId = false;
    for (String key : shardKey.keySet()) {
        shardKeyFilter.put(key, 1);
        if (key.equals("_id")) {
            hasId = true;
        }
    }
    if (!hasId) {
        shardKeyFilter.put("_id", 0);
    }

    dia = (FormDialog) getBoundUnit(Item.regenConfirm);
    if (!dia.show()) {
        return;
    }

    // now fetch all records from each shard
    final AtomicInteger todo = new AtomicInteger(mongoToShard.size());
    for (Map.Entry<MongoClient, String> entry : mongoToShard.entrySet()) {
        final MongoClient mongo = entry.getKey();
        final String shard = entry.getValue();
        new DbJob() {

            @Override
            public Object doRun() throws Exception {
                BasicDBObject shardObj = (BasicDBObject) shardList.get(shard);
                long count = mongo.getDB(db).getCollection(col).count();
                shardObj.put("count", count);
                DBCursor cur = mongo.getDB(db).getCollection(col).find(new BasicDBObject(), shardKeyFilter);
                long i = 0;
                int inserted = 0;
                long start = System.currentTimeMillis();
                while (cur.hasNext() && !isCancelled()) {
                    BasicDBObject key = (BasicDBObject) cur.next();
                    setProgress((int) ((++i * 100.0f) / count));
                    try {
                        BasicDBObject entry = new BasicDBObject("_id", key);
                        entry.put("_shard", shard);
                        tmpchunks.insert(entry);
                        ++inserted;
                    } catch (Exception e) {
                        getLogger().log(Level.WARNING, e.getMessage(), e);
                    }
                }

                if (isCancelled()) {
                    shardObj.put("cancelled", true);
                }
                shardObj.put("inserted", inserted);
                shardObj.put("scanTime", System.currentTimeMillis() - start);
                todo.decrementAndGet();
                return null;
            }

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

            @Override
            public String getShortName() {
                return "Scanning " + shard;
            }

            @Override
            public boolean isDeterminate() {
                return true;
            }
        }.addJob();

    }

    new DbJob() {

        @Override
        public Object doRun() throws Exception {
            // wait for all shards to be done
            long start = System.currentTimeMillis();
            while (todo.get() > 0 && !isCancelled()) {
                Thread.sleep(2000);
            }

            if (isCancelled()) {
                result.put("cancelled", true);
                return result;
            }

            // find highest current timestamp
            DBCursor cur = chunks.find().sort(new BasicDBObject("lastmod", -1)).batchSize(-1);
            BasicDBObject chunk = (BasicDBObject) (cur.hasNext() ? cur.next() : null);
            BSONTimestamp ts = (BSONTimestamp) (chunk != null ? chunk.get("lastmod") : null);

            // now infer chunk ranges
            long count = tmpchunks.count();
            result.put("uniqueKeys", count);
            int numChunks = 0;
            cur = tmpchunks.find().sort(new BasicDBObject("_id", 1));
            BasicDBObject prev = (BasicDBObject) cur.next();
            BasicDBObject next = null;
            // snap prev to minkey
            BasicDBObject theid = (BasicDBObject) prev.get("_id");
            for (String key : shardKey.keySet()) {
                theid.put(key, new MinKey());
            }
            String currentShard = prev.getString("_shard");

            int i = 1;
            while (cur.hasNext()) {
                next = (BasicDBObject) cur.next();
                setProgress((int) ((++i * 100.0f) / count));
                String newShard = next.getString("_shard");
                if (newShard.equals(currentShard))
                    continue;

                // add chunk
                ts = getNextTimestamp(ts);
                chunk = getChunk(ns, shardKey, prev, next, ts);
                chunks.insert(chunk);
                prev = next;
                currentShard = prev.getString("_shard");
                ++numChunks;
            }

            // build max
            next = new BasicDBObject();
            for (String key : shardKey.keySet()) {
                next.put(key, new MaxKey());
            }
            next = new BasicDBObject("_id", next);
            ts = getNextTimestamp(ts);
            chunk = getChunk(ns, shardKey, prev, next, ts);
            chunks.insert(chunk);
            ++numChunks;
            result.put("numChunks", numChunks);
            result.put("totalTime", System.currentTimeMillis() - start);
            return result;
        }

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

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

        @Override
        public boolean isDeterminate() {
            return true;
        }
    }.addJob();
}

From source file:com.epam.dlab.migration.mongo.changelog.DlabChangeLog.java

License:Apache License

@ChangeSet(order = "001", id = "001", author = "bhliva")
public void migrateSchedulerFields(DB db) {
    log.info("Replacing field days_repeat with start_days_repeat and stop_days_repeat");
    final DBCollection userInstances = db.getCollection("userInstances");

    StreamSupport.stream(userInstances.find().spliterator(), false)
            .forEach(dbObject -> updateSchedulerFieldsForExploratory(userInstances, dbObject));
    log.info("Replacing scheduler field days_repeat finished successfully");
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Mongo ID generation like it is done in the grails gorm framework
 *
 * @param collectionName//from  ww  w. j a va  2  s.c  o  m
 *            The name of the collection the id should be generated for
 * @param db
 *            The mongodb connection
 * @return a new id
 */
private Long generateIdentifier(String collectionName, DB db) {

    // get or create the Collection for the ID storage
    DBCollection dbCollection = db.getCollection(collectionName + ".next_id");
    // create entry to store the newly generated id
    DBObject nativeEntry = new BasicDBObject();

    while (true) {
        DBCursor result = dbCollection.find().sort(new BasicDBObject("_id", -1)).limit(1);

        long nextId;
        if (result.hasNext()) {
            final Long current = (Long) result.next().get("_id");
            nextId = current + 1;
        } else {
            nextId = 1;
        }

        nativeEntry.put("_id", nextId);
        final WriteResult writeResult = dbCollection.insert(nativeEntry);
        final CommandResult lastError = writeResult.getLastError();
        if (lastError.ok()) {
            break;
        }

        final Object code = lastError.get("code");
        // duplicate key error try again
        if (code != null && code.equals(11000)) {
            continue;
        }
        break;
    }

    return (Long) nativeEntry.get("_id");
}

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

public String query() {
    String records = "<table>" + Utility.formTableHead() + "<tbody>";

    DB myDB = getDB(DB_NAME);// www.  j  av a 2 s  .  com
    DBCollection coll = myDB.getCollection("projects");

    DBCursor ret = coll.find();
    BasicDBObject sort = new BasicDBObject("PROJECT_ID", 1);
    ret.sort(sort);

    try {
        while (ret.hasNext()) {
            records = records + "<tr>";

            DBObject rec = (DBObject) ret.next();
            Iterator keys = Utility.DATA_KEYS.iterator();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                String value = (String) rec.get(key);
                if (key.equals("PROJECT_ID")) {
                    records = records + "<td><a href=\"" + Utility.PROJECT_URL_PREFIX + value + "\">" + value
                            + "</td>";
                } else {
                    records = records + "<td>" + value + "</td>";

                }
            }

            records = records + "</tr>";
            //System.out.println(rec);
        }
    } finally {
        ret.close();
    }

    records = records + "</tbody></table>";
    return records;
}

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

public String query(String condition) {
    List<DBObject> list = null;

    DB myDB = getDB(DB_NAME);//from w ww .  j  a  v a  2 s  .  c om
    DBCollection coll = myDB.getCollection("projects");

    DBCursor ret = coll.find();
    BasicDBObject sort = new BasicDBObject("PROJECT_ID", 1);
    ret.sort(sort);

    try {
        list = ret.toArray();
    } finally {
        ret.close();
    }

    System.out.println("quering...." + condition);
    return list.toString();
}

From source file:com.fliker.Modal.GuidancePreview.java

public ArrayList getGuidance(String lastid) {

    ArrayList postlist = new ArrayList();

    MongoConnection mongoconn = new MongoConnection();
    DBCollection collection = mongoconn.getDBConnection("GuidanceSelection");
    DBCursor cursor;//from   w  w  w .j a  v  a2 s  .c o m
    if (lastid.isEmpty()) {
        cursor = collection.find().limit(50).sort(new BasicDBObject("profileid", -1));
    } else {
        cursor = collection.find(new BasicDBObject("profileid", lastid)).limit(50)
                .sort(new BasicDBObject("profileid", -1));
    }

    while (cursor.hasNext()) {
        postlist.add(cursor.next());
    }

    return postlist;
}

From source file:com.fliker.Modal.GuidancePreview.java

public ArrayList getGuidanceResources(String subject, String guidancetype) {

    /*ArrayList postlist = new ArrayList();
            //  w ww . ja v a2s  . com
    MongoConnection mongoconn = new MongoConnection();
    DBCollection collection = mongoconn.getDBConnection("GuidanceSelection");
    DBCursor cursor;
    if(lastid.isEmpty()){
       cursor = collection.find().limit(50).sort(new BasicDBObject("guidanceid",-1));
    }else{
       cursor = collection.find(new BasicDBObject("guidanceid", lastid)).limit(50).sort(new BasicDBObject("guidanceid",-1));
    }
            
    while(cursor.hasNext()){
       postlist.add(cursor.next());
    }
            
    ArrayList guidancelist =  new ArrayList();
            
    for (int i = 0; i < postlist.size(); i++) {
       System.out.println("postlist.size() ><><" + postlist.size());
       HashMap perPostSet = (HashMap) postlist.get(i);
       Set perset = perPostSet.entrySet();
       Iterator perit = perset.iterator();
       while (perit.hasNext()) {
            
    Map.Entry perme = (Map.Entry) perit.next();
            
    String keyvalue = (String) perme.getKey();
            
    System.out.println(perme.getValue());
            
    if (keyvalue.equalsIgnoreCase("guidanceSubject")) {
               
       BasicDBList basicdb = (BasicDBList)perme.getValue();
               
       for(int m = 0;m< basicdb.size(); m++){
          String guidencesub = (String)basicdb.get(m);
          if(guidencesub.equalsIgnoreCase(subject)){
             guidancelist.add(postlist.get(i));
          }
          //System.out.println(imageid);
       }
               
    }
       }
               
    }
            
            
    return guidancelist;*/

    ArrayList guidancelist = new ArrayList<Post>();

    MongoConnection mongoconn = new MongoConnection();
    DBCollection collection = mongoconn.getDBConnection("GuidanceSelection");
    DBCursor cursor;
    cursor = collection.find().limit(20).sort(new BasicDBObject("guidanceid", -1));

    while (cursor.hasNext()) {

        DBObject dbj = cursor.next();
        HashMap totalSet = new HashMap();

        if (((dbj.get("guidanceSubject").toString()).equalsIgnoreCase(subject))
                && ((dbj.get("guidencetype").toString()).equalsIgnoreCase(guidancetype))) {
            totalSet.put("guidanceid", (String) dbj.get("guidanceid"));
            totalSet.put("guidanceSubject", (String) dbj.get("guidanceSubject"));
            totalSet.put("guidanceflag", (String) dbj.get("guidanceflag"));
            totalSet.put("guidencetype", (String) dbj.get("guidencetype"));
            ProfilePreview profprev = new ProfilePreview();
            ArrayList profileinfo = profprev.getProfileInfo((String) dbj.get("userid"));
            for (int m = 0; m < profileinfo.size(); m++) {

                if (profileinfo.get(m) instanceof Profile) {
                    Profile profileinfos = (Profile) profileinfo.get(m);

                    totalSet.put("profileid", (String) dbj.get("profileid"));
                    totalSet.put("profileImage", (String) dbj.get("profileImageid"));
                    totalSet.put("profileName", (String) dbj.get("name"));

                }

            }

            totalSet.put("userid", (String) dbj.get("userid"));

        }

        guidancelist.add(totalSet);
    }

    return guidancelist;

}

From source file:com.fliker.Modal.GuidancePreview.java

public ArrayList getNextSetGuidanceResources(String pageno, String subject) {

    ArrayList guidancelist = new ArrayList<Post>();

    MongoConnection mongoconn = new MongoConnection();
    DBCollection collection = mongoconn.getDBConnection("GuidanceSelection");
    DBCursor cursor;//w w w. j  a v a 2  s  .  c o m
    int pagenos = Integer.parseInt(pageno);
    cursor = collection.find().skip(20 * pagenos).limit(20).sort(new BasicDBObject("guidanceid", -1));

    while (cursor.hasNext()) {

        DBObject dbj = cursor.next();
        HashMap totalSet = new HashMap();

        if ((dbj.get("guidanceflag").toString()).equalsIgnoreCase(subject)) {
            totalSet.put("guidanceid", dbj.get("guidanceid"));
            totalSet.put("guidanceSubject", dbj.get("guidanceSubject"));
            totalSet.put("guidanceflag", dbj.get("guidanceflag"));
            totalSet.put("guidencetype", dbj.get("guidencetype"));
        }

        guidancelist.add(totalSet);
    }

    return guidancelist;

}

From source file:com.fliker.Modal.OSMPreview.java

public ArrayList getProjectInfoList() {

    ArrayList projectlist = new ArrayList();
    MongoConnection mongoconn = new MongoConnection();
    DBCollection collection = mongoconn.getDBConnection("OSMOwner");
    DBCursor cursor;/*from   www.j a va  2s  .co m*/

    cursor = collection.find().sort(new BasicDBObject("osmmodelid", -1));
    while (cursor.hasNext()) {
        DBObject currentItem = cursor.next();
        String osmmodelid = (String) currentItem.get("osmmodelid");

        MongoConnection mongoconinner = new MongoConnection();
        DBCursor resultcursor = mongoconinner.getDBObject("osmmodelid", osmmodelid, "OSMProjectInfo");
        if (resultcursor.hasNext()) {

            DBObject theObjosm = resultcursor.next();

            OSMProjectInfo osmprojinf = new OSMProjectInfo();
            osmprojinf.setOsmmodelid((String) theObjosm.get("osmmodelid"));
            osmprojinf.setProjectbuyers((String[]) theObjosm.get("projectbuyers"));
            osmprojinf.setProjectdemandchart((String) theObjosm.get("projectdemandchart"));
            osmprojinf.setProjectdescription((String) theObjosm.get("projectdescription"));
            osmprojinf.setProjectdocs((String[]) theObjosm.get("projectdocs"));
            osmprojinf.setProjectinfoid((String) theObjosm.get("projectinfoid"));
            osmprojinf.setProjectinvestors((String[]) theObjosm.get("projectinvestors"));
            osmprojinf.setProjectmarkettingdoc((String) theObjosm.get("projectmarkettingdoc"));
            osmprojinf.setProjectname((String) theObjosm.get("projectname"));
            osmprojinf.setProjectresources((String[]) theObjosm.get("projectresources"));
            osmprojinf.setProjectstockprice((String) theObjosm.get("projectstockprice"));
            osmprojinf.setProjectrequestors((String[]) theObjosm.get("projectrequestors"));
            projectlist.add(osmprojinf);

        }

    }

    return projectlist;
}

From source file:com.fliker.Modal.OSMPreview.java

public ArrayList getAllCompanyList() {
    // TODO Auto-generated method stub

    ArrayList companylist = new ArrayList();

    MongoConnection mongoconn = new MongoConnection();
    DBCollection collection = mongoconn.getDBConnection("OSMOwner");
    DBCursor cursor;/*from  w w  w .  java2 s . c  o  m*/

    cursor = collection.find().sort(new BasicDBObject("osmmodelid", -1));
    while (cursor.hasNext()) {
        DBObject currentItem = cursor.next();

        Company comapny = new Company();
        comapny.setCompanyid((String) currentItem.get("companyid"));
        comapny.setAboutUs((String) currentItem.get("AboutUs"));
        comapny.setAboutvideoid((String) currentItem.get("aboutvideoid"));
        comapny.setCompanyImageid((String) currentItem.get("companyImageid"));
        comapny.setCompanyIndustry((String) currentItem.get("companyIndustry"));
        comapny.setCompanylogoid((String) currentItem.get("companylogoid"));
        comapny.setCompanyname((String) currentItem.get("companyname"));
        comapny.setCompanyWebsite((String) currentItem.get("companyWebsite"));

        companylist.add(comapny);

    }

    return companylist;
}