Example usage for com.mongodb DBCollection count

List of usage examples for com.mongodb DBCollection count

Introduction

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

Prototype

public long count() 

Source Link

Document

Same as #getCount()

Usage

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

public static void main(final String[] args) {
    if (args.length < 3) {
        usage();//from   w  w w .  ja  va  2  s. co  m
    }

    String isisMaster = null;
    String mongoDbName = null;
    String mongoCollection = null;
    String host = "localhost";
    int port = DEFAULT_PORT;
    String user = null;
    String password = null;
    String encoding = Master.GUESS_ISO_IBM_ENCODING;
    int from = 1;
    int to = Integer.MAX_VALUE;
    int idTag = 0;
    int tell = Integer.MAX_VALUE;
    boolean useFDT = false;
    boolean useOnlyFDT = false;
    boolean clearCollection = false;

    for (String arg : args) {
        if (arg.startsWith("-isisMaster=")) {
            isisMaster = arg.substring(12);
        } else if (arg.startsWith("-mongoDbName=")) {
            mongoDbName = arg.substring(13);
        } else if (arg.startsWith("-collection=")) {
            mongoCollection = arg.substring(12);
        } else if (arg.startsWith("-mongoHost=")) {
            host = arg.substring(11);
        } else if (arg.startsWith("-mongoPort=")) {
            port = Integer.parseInt(arg.substring(11));
        } else if (arg.startsWith("-user=")) {
            user = arg.substring(6);
        } else if (arg.startsWith("-password=")) {
            password = arg.substring(10);
        } else if (arg.startsWith("-encod=")) {
            encoding = arg.substring(7);
        } else if (arg.startsWith("-from=")) {
            from = Integer.parseInt(arg.substring(6));
        } else if (arg.startsWith("-to=")) {
            to = Integer.parseInt(arg.substring(4));
        } else if (arg.startsWith("-idTag=")) {
            idTag = Integer.parseInt(arg.substring(7));
        } else if (arg.startsWith("-tell=")) {
            tell = Integer.parseInt(arg.substring(6));
        } else if (arg.equals("--useFDT")) {
            useFDT = true;
        } else if (arg.equals("--useOnlyFDT")) {
            useOnlyFDT = true;
        } else if (arg.equals("--clearCollection")) {
            clearCollection = true;
        } else {
            usage();
        }
    }

    final Logger log = Logger.getLogger("Isis2Mongo");
    final List<DBObject> buffer = new ArrayList<DBObject>();

    try {
        final DB database = getDatabase(host, port, user, password, mongoDbName);
        final DBCollection collection = database.getCollection(mongoCollection);
        final Master mst = MasterFactory.getInstance(isisMaster).setEncoding(encoding).open();
        final boolean hasFdt = new File(Util.changeFileExtension(isisMaster, ZeFDT.DEFAULT_EXTENSION)).isFile();
        final ZeFDT fdt = ((useFDT || useOnlyFDT) && hasFdt) ? new ZeFDT().fromFile(isisMaster) : null;
        final Map<Integer, ZeFDT.ZeFDTField> mfdt = (fdt == null) ? null : fdt.getFieldDescriptionMapEx();

        to = Math.min(to, mst.getControlRecord().getNxtmfn() - 1);
        int cur = 0;

        if (clearCollection) {
            collection.drop();
        }

        for (int mfn = from; mfn <= to; mfn++) {
            final Record rec = mst.getRecord(mfn);

            if (rec.getStatus() == Record.Status.ACTIVE) {
                buffer.add(createDocument(rec, idTag, mfdt, useOnlyFDT));
                if (buffer.size() == DEFAULT_BUFFER_SIZE) {
                    final String errMess = collection.insert(buffer).getError();
                    if (errMess != null) {
                        log.warning(errMess);
                    }
                    buffer.clear();
                }
            }

            if (++cur == tell) {
                cur = 0;
                log.info("+++ " + Integer.toString(mfn));
            }
        }
        if (!buffer.isEmpty()) {
            final String errMess = collection.insert(buffer).getError();
            if (errMess != null) {
                log.warning(errMess);
            }
        }

        mst.close();
        log.info("Importing ");
        log.info(args[0]);
        log.info(" finished.");
        log.info("Documents imported: ");
        log.info(Integer.toString(cur));
        log.info("Total documents : ");
        log.info(Long.toString(collection.count()));
    } catch (Exception ex) {
        log.severe(ex.getMessage());
    }
}

From source file:cluster.StressNode.java

License:Apache License

public static void main(String[] args) throws Exception {
    String home = "";
    try {/* w w w  .  j  a va  2  s  .  co  m*/
        home = new File(StressNode.class.getResource("/").toURI()).getAbsolutePath();
    } catch (URISyntaxException e) {
    }
    System.setProperty("s1." + OptionsStorage.CONFIG_HOME, home + "/options");

    new ClusterLifecycleAction().start();

    try {
        final String collection = "stress1";
        DBCollection coll = MongoDBConnectionHelper.getConnection(null).getCollection(collection);
        System.out.println("count:>>>>>>>>>>>>>" + coll.count());
        if ((coll.count() % 5) != 0) {
            throw new S1SystemError("something goes wrong!!!");
        }

        String id = null;
        try {
            id = Transactions.begin();

            for (int i = 0; i < 5; i++) {
                System.out.println(">>" + i);
                MongoDBDDS.add(new Id(null, collection, "" + i),
                        Objects.newHashMap(String.class, Object.class, "name", "test_" + i));
                if (i == 3) {
                    //throw new RuntimeException("test");
                    //System.exit(-1);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }

            Transactions.commit(id);
        } catch (Throwable e) {
            Transactions.rollbackOnError(id, e);
        } finally {

        }
    } finally {
        new ClusterLifecycleAction().stop();
        Hazelcast.shutdownAll();
    }
}

From source file:com.andreig.jetty.CollectionsServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doGet()");

    if (!can_read(req)) {
        res.sendError(SC_UNAUTHORIZED);/*from   w w w .java 2  s. c o m*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String op = req.getParameter("op");
    if (db_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (op == null)
        op = "list";

    DB db = mongo.getDB(db_name);

    if ("list".equals(op)) {
        Set<String> cols = db.getCollectionNames();
        out_json(req, cols);
        return;
    }

    // requires colname
    String col_name = req.getParameter("colname");
    if (col_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }
    DBCollection col = db.getCollection(col_name);

    if ("count".equals(op)) {
        out_str(req, "{\"count\":" + col.count() + "}", "application/json");
    } else if ("stats".equals(op)) {
        BasicBSONObject o = col.getStats();
        out_str(req, o.toString(), "application/json");
        return;
    } else
        res.sendError(SC_BAD_REQUEST);

}

From source file:com.bbc.remarc.ws.MetadataService.java

License:Apache License

/**
 * Core random find service for entities that have metadata
 * //from   ww w  .j  av a2s .co m
 * @param db
 *            db instance
 * @param limit
 *            number of records to find
 * @param filter
 *            find records that match this filter. Can be null for no
 *            filtering
 * @param collectionName
 *            the database collection name
 * @return collection of found DBObjects
 */
protected List<DBObject> findRandom(DB db, long limit, DBObject filter, final String collectionName) {

    List<DBObject> results = new ArrayList<DBObject>();

    try {
        db.requestStart();
        Random random = new Random();
        DBCollection dbCollection = db.getCollection(collectionName);

        long maxRecords = dbCollection.count();

        if (limit > maxRecords) {
            limit = maxRecords;
        }

        for (int i = 0; i < limit; i++) {

            boolean foundUnique = false;

            int numChecked = 0;

            while (!foundUnique) {

                if (numChecked == limit) {
                    break;
                }

                int randomNumber = random.nextInt((int) dbCollection.count());

                DBObject randomRecord = null;

                if (filter == null) {
                    randomRecord = dbCollection.find().limit(-1).skip(randomNumber).next();
                } else {

                    DBCursor cursor = dbCollection.find(filter).limit(-1);

                    if (i >= cursor.count()) {
                        // we dont have enough records from the filter to
                        // match the limit
                        break;
                    }

                    randomNumber = random.nextInt((int) cursor.count());

                    randomRecord = cursor.skip(randomNumber).next();
                }

                foundUnique = resultsContains(randomRecord, results) == false;

                if (foundUnique) {
                    randomRecord.removeField("_id");
                    results.add(randomRecord);
                }
            }
        }
    } finally {
        db.requestDone();
    }
    return results;
}

From source file:com.cyslab.craftvm.rest.mongo.CollectionsServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.trace("doGet()");

    if (!can_read(req)) {
        res.sendError(SC_UNAUTHORIZED);//from  w  w  w.j  av  a 2 s. c o m
        return;
    }

    String db_name = req.getParameter("dbname");
    String op = req.getParameter("op");
    if (db_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (op == null)
        op = "list";

    DB db = mongo.getDB(db_name);

    if ("list".equals(op)) {
        Set<String> cols = db.getCollectionNames();
        out_json(req, cols);
        return;
    }

    // requires colname
    String col_name = req.getParameter("colname");
    if (col_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }
    DBCollection col = db.getCollection(col_name);

    if ("count".equals(op)) {
        out_str(req, "{\"count\":" + col.count() + "}", "application/json");
    } else if ("stats".equals(op)) {
        BasicBSONObject o = col.getStats();
        out_str(req, o.toString(), "application/json");
        return;
    } else
        res.sendError(SC_BAD_REQUEST);

}

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;/* ww  w .  j  av a2s .c om*/
    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.ReplSetPanel.java

License:Apache License

public void compareReplicas(ButtonBase button) {
    final String stat = getStringFieldValue(Item.crStat);
    new DbJob() {

        @Override/*from w  ww  . j  a va 2s .c  o m*/
        public Object doRun() {
            ReplSetNode node = getReplSetNode();
            if (!node.hasChildren())
                return null;

            ArrayList<MongoClient> svrs = new ArrayList<MongoClient>();
            for (XmlUnit unit : node.getChildren()) {
                ServerNode svr = (ServerNode) unit;
                MongoClient svrm = svr.getServerMongoClient();
                try {
                    svrm.getDatabaseNames();
                } catch (Exception e) {
                    continue;
                }
                svrs.add(svrm);
            }

            BasicDBObject res = new BasicDBObject();
            MongoClient m = getReplSetNode().getMongoClient();
            for (String dbname : m.getDatabaseNames()) {
                DB db = m.getDB(dbname);
                BasicDBObject dbres = new BasicDBObject();
                for (String colname : db.getCollectionNames()) {
                    DBCollection col = db.getCollection(colname);
                    BasicDBObject colres = new BasicDBObject();
                    BasicDBObject values = new BasicDBObject();
                    boolean same = true;
                    long ref = -1;
                    for (MongoClient svrm : svrs) {
                        DBCollection svrcol = svrm.getDB(dbname).getCollection(colname);
                        long value = 0;
                        if (stat.startsWith("Count")) {
                            value = svrcol.count();
                        } else if (stat.startsWith("Data Size")) {
                            CommandResult stats = svrcol.getStats();
                            value = stats.getLong("size");
                        }
                        values.append(svrm.getConnectPoint(), value);
                        if (ref < 0)
                            ref = value;
                        else if (ref != value)
                            same = false;
                    }
                    if (!same) {
                        colres.append("values", values);
                        dbres.append(colname, colres);
                    }
                }
                if (!dbres.isEmpty()) {
                    res.append(dbname, dbres);
                }
            }

            return res;
        }

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

        @Override
        public String getShortName() {
            return "Compare Replicas";
        }
    }.addJob();
}

From source file:com.englishtown.vertx.GridFSModule.java

License:Open Source License

public void saveFile(Message<JsonObject> message, JsonObject jsonObject) {

    ObjectId id = getObjectId(message, jsonObject, "id");
    if (id == null) {
        return;/* w ww.  ja  va 2s.c o m*/
    }

    Integer length = getRequiredInt("length", message, jsonObject, 1);
    if (length == null) {
        return;
    }

    Integer chunkSize = getRequiredInt("chunkSize", message, jsonObject, 1);
    if (chunkSize == null) {
        return;
    }

    long uploadDate = jsonObject.getLong("uploadDate", 0);
    if (uploadDate <= 0) {
        uploadDate = System.currentTimeMillis();
    }

    String filename = jsonObject.getString("filename");
    String contentType = jsonObject.getString("contentType");
    JsonObject metadata = jsonObject.getObject("metadata");

    try {
        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("_id", id).add("length", length)
                .add("chunkSize", chunkSize).add("uploadDate", new Date(uploadDate));

        if (filename != null)
            builder.add("filename", filename);
        if (contentType != null)
            builder.add("contentType", contentType);
        if (metadata != null)
            builder.add("metadata", JSON.parse(metadata.encode()));

        DBObject dbObject = builder.get();

        String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET);
        DBCollection collection = db.getCollection(bucket + ".files");

        // Ensure standard indexes as long as collection is small
        if (collection.count() < 1000) {
            collection.ensureIndex(BasicDBObjectBuilder.start().add("filename", 1).add("uploadDate", 1).get());
        }

        collection.save(dbObject);
        sendOK(message);

    } catch (Exception e) {
        sendError(message, "Error saving file", e);
    }
}

From source file:com.englishtown.vertx.GridFSModule.java

License:Open Source License

public void saveChunk(Message<Buffer> message, JsonObject jsonObject, byte[] data) {

    if (data == null || data.length == 0) {
        sendError(message, "chunk data is missing");
        return;//from  w ww.  j  a  v  a 2  s  . co  m
    }

    ObjectId id = getObjectId(message, jsonObject, "files_id");
    if (id == null) {
        return;
    }

    Integer n = getRequiredInt("n", message, jsonObject, 0);
    if (n == null) {
        return;
    }

    try {
        DBObject dbObject = BasicDBObjectBuilder.start().add("files_id", id).add("n", n).add("data", data)
                .get();

        String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET);
        DBCollection collection = db.getCollection(bucket + ".chunks");

        // Ensure standard indexes as long as collection is small
        if (collection.count() < 1000) {
            collection.ensureIndex(BasicDBObjectBuilder.start().add("files_id", 1).add("n", 1).get(),
                    BasicDBObjectBuilder.start().add("unique", 1).get());
        }

        collection.save(dbObject);
        sendOK(message);

    } catch (RuntimeException e) {
        sendError(message, "Error saving chunk", e);
    }

}

From source file:com.garyclayburg.persistence.repository.DummyUserRepository.java

License:Open Source License

/**
 * Retrieves basic metadata without using springdata support
 *
 * @param databaseName//from   ww w .j  a  v  a 2 s .  c om
 *
 * @return
 */
public long countUnderAgeTmp(String databaseName) {
    DB db = mongoClient.getDB(databaseName);
    DBCollection collection = db.getCollection("user");
    return collection.count();
}