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:org.aw20.mongoworkbench.command.CollectionRemoveAllMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);//from  w w w.j a  v  a 2 s.c om

    DBCollection collection = db.getCollection(sColl);
    collection.remove(new BasicDBObject());

    setMessage("count=" + collection.count());
}

From source file:org.einherjer.week2.samples.FindSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("findSample");
    collection.drop();/*from  ww w .  j  a  va  2s .c om*/

    // insert 10 documents with a random integer as the value of field "x"
    for (int i = 0; i < 10; i++) {
        collection.insert(new BasicDBObject("x", new Random().nextInt(100)));
    }

    System.out.println("Find one:");
    DBObject one = collection.findOne();
    System.out.println(one);

    System.out.println("\nFind all: ");
    DBCursor cursor = collection.find();
    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            System.out.println(cur);
        }
    } finally {
        cursor.close();
    }

    System.out.println("\nCount:");
    long count = collection.count();
    System.out.println(count);
}

From source file:org.exist.mongodb.xquery.mongodb.collection.Count.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*from   w  w w  .j av a2  s . c  o m*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        // Get query when available
        String query = (args.length == 4) ? args[3].itemAt(0).getStringValue() : null;
        BasicDBObject mongoQuery = (query == null) ? null : (BasicDBObject) JSON.parse(query);

        // Get database and collection
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Count documents
        Long nrOfDocuments = (mongoQuery == null) ? dbcol.count() : dbcol.count(mongoQuery);

        return new IntegerValue(nrOfDocuments);

    } catch (JSONParseException ex) {
        String msg = "Invalid JSON data: " + ex.getMessage();
        LOG.error(msg);
        throw new XPathException(this, MongodbModule.MONG0004, msg);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw new XPathException(this, MongodbModule.MONG0003, t.getMessage());
    }

}

From source file:org.mongoj.samples.service.persistence.CarPersistenceImpl.java

License:Open Source License

public long countAll() throws SystemException {
    DBCollection collection = getDB().getCollection(CarImpl.COLLECTION_NAME);

    return collection.count();
}

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

public long countAll() throws SystemException {
    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    return collection.count();
}

From source file:org.mongoste.core.impl.mongodb.MongoStatsEngine.java

License:Open Source License

protected DBCollection getStatsCollection(StatEvent event, TimeScope timeScope) throws StatsEngineException {
    String name = getScopeCollectionName(COLLECTION_STATS, event, timeScope);
    DBCollection stats = collectionMap.get(name);
    if (stats == null) {
        stats = db.getCollection(name);//from w  ww. ja  v  a2s. c o m
        if (stats.count() != 0) {
            try {
                MongoUtil.createIndexes(stats, "value.count", "value.unique");
            } catch (MongoException ex) {
                throw new StatsEngineException("creating collection " + name + " indexes", ex);
            }
        }
        collectionMap.put(name, stats);
    }
    return stats;
}

From source file:org.vertx.mods.MongoPersistor.java

License:Apache License

private void doCount(Message<JsonObject> message) {
    String collection = getMandatoryString("collection", message);
    if (collection == null) {
        return;/*from  ww  w .j  a va2 s. c o  m*/
    }
    JsonObject matcher = message.body.getObject("matcher");
    DBCollection coll = db.getCollection(collection);
    long count;
    if (matcher == null) {
        count = coll.count();
    } else {
        count = coll.count(jsonToDBObject(matcher));
    }
    JsonObject reply = new JsonObject();
    reply.putNumber("count", count);
    sendOK(message, reply);
}

From source file:redmart.crm.product.dao.ProductDAO.java

public long getProductCount() {
    _logger.info("Calling getProductCount");
    DBCollection dbCollection = datastore.getCollection(Product.class);
    long count = dbCollection.count();
    _logger.info("Exiting getProductCount");
    return count;
}

From source file:simple.crawler.mongo.CrawlingDB.java

License:Open Source License

public long count(Collection collection) {
    DB db = client.getDB(dbName);//from ww w .j a  v a 2  s  . c o  m
    DBCollection con = db.getCollection(collection.toString());
    return con.count();
}

From source file:uk.ac.ebi.eva.test.utils.DropStudyJobTestUtils.java

License:Apache License

public static void assertDropSingleStudy(DBCollection variantsCollection, String studyId,
        long expectedVariantsAfterDropStudy) {

    assertEquals(expectedVariantsAfterDropStudy, variantsCollection.count());

    BasicDBObject singleStudyVariants = new BasicDBObject(FILES_STUDY_ID_FIELD, studyId).append(FILES_FIELD,
            new BasicDBObject("$size", 1));
    assertEquals(0, variantsCollection.count(singleStudyVariants));
}