Example usage for com.mongodb DBCollection getCount

List of usage examples for com.mongodb DBCollection getCount

Introduction

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

Prototype

public long getCount(@Nullable final DBObject query) 

Source Link

Document

Get the count of documents in collection that would match a criteria.

Usage

From source file:applango.common.services.DB.mongo.mongoDB.java

public static void checkRecordCreatedInDB(Applango applango, String connection, DBCollection coll,
        DBObject dbObjectRecordQuery) {/*www  . j  av  a2  s . c o m*/
    logger.info("Check that record created in DB table : " + connection);
    Assert.assertTrue(coll.getCount(dbObjectRecordQuery) == 1);
}

From source file:applango.common.services.DB.mongo.mongoDB.java

protected static void checkNRecordsCreatedInDB(Applango applango, String connection, DBCollection coll,
        DBObject dbObjectRecordQuery, int n) {
    logger.info("Check that record created in DB table : " + connection);
    Assert.assertTrue(coll.getCount(dbObjectRecordQuery) == n);
}

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

License:Apache License

public void update(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.upQuery)).getDBObject();
    final BasicDBObject update = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.upUpdate)).getDBObject();
    final boolean upsert = getBooleanFieldValue(Item.upUpsert);
    final boolean multi = getBooleanFieldValue(Item.upMulti);
    final boolean safe = getBooleanFieldValue(Item.upSafe);
    col.setWriteConcern(WriteConcern.SAFE);

    new DbJob() {
        @Override/*from   w ww.j  av a2s .  c  om*/
        public Object doRun() {
            if (safe) {
                long count = col.getCount(query);
                long toupdate = count > 0 ? 1 : 0;
                if (multi) {
                    toupdate = count;
                }
                String text = "Proceed with updating " + toupdate + " of " + count + " documents?";
                ConfirmDialog confirm = new ConfirmDialog(null, "Confirm Update", null, text);
                if (!confirm.show()) {
                    return null;
                }
            }
            return col.update(query, (DBObject) update.copy(), upsert, multi);
        }

        @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);
            obj.put("upsert", upsert);
            obj.put("multi", multi);
            return obj;
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }
    }.addJob();
}

From source file:org.chililog.server.data.RepositoryConfigController.java

License:Apache License

/**
 * Saves the repository into mongoDB//  w  w  w  .  java  2  s  . c  om
 * 
 * @param db
 *            MongoDb connection
 * @param repoConfig
 *            Repository configuration to save
 * @throws ChiliLogException
 *             if there are errors
 */
public void save(DB db, RepositoryConfigBO repoConfig) throws ChiliLogException {
    // Validate unique name
    DBCollection coll = db.getCollection(MONGODB_COLLECTION_NAME);
    BasicDBObject condition = new BasicDBObject();
    condition.put(RepositoryConfigBO.NAME_FIELD_NAME, repoConfig.getName());
    if (repoConfig.isExistingRecord()) {
        condition.put(BO.DOCUMENT_ID_FIELD_NAME, new BasicDBObject("$ne", repoConfig.getDocumentID()));
    }
    long i = coll.getCount(condition);
    if (i > 0) {
        throw new ChiliLogException(Strings.REPO_INFO_DUPLICATE_NAME_ERROR, repoConfig.getName());
    }

    // Validate unique parser names
    for (RepositoryParserConfigBO p : repoConfig.getParsers()) {
        String parserName = p.getName();
        int pCount = 0;
        for (RepositoryParserConfigBO p2 : repoConfig.getParsers()) {
            if (p2.getName().equals(parserName)) {
                pCount++;
            }
        }
        if (pCount != 1) {
            throw new ChiliLogException(Strings.REPO_INFO_DUPLICATE_PARSER_NAME_ERROR, parserName,
                    repoConfig.getName());
        }

        // Validate unique field names per parser
        for (RepositoryFieldConfigBO f : p.getFields()) {
            String fieldName = f.getName();
            int count = 0;
            for (RepositoryFieldConfigBO f2 : p.getFields()) {
                if (f2.getName().equals(fieldName)) {
                    count++;
                }
            }
            if (count != 1) {
                throw new ChiliLogException(Strings.REPO_INFO_DUPLICATE_FIELD_NAME_ERROR, fieldName, parserName,
                        repoConfig.getName());
            }
        }
    }

    // Save it
    super.save(db, repoConfig);

    // Add repository and index
    createIndexes(db);
}

From source file:org.chililog.server.data.UserController.java

License:Apache License

/**
 * Saves the user into mongoDB//from w w  w  . j  a  v a2 s .  c  om
 * 
 * @param db
 *            MongoDb connection
 * @param user
 *            User to save
 * @throws ChiliLogException
 *             if there are errors
 */
public void save(DB db, UserBO user) throws ChiliLogException {
    // Validate unique username
    DBCollection coll = db.getCollection(MONGODB_COLLECTION_NAME);
    BasicDBObject condition = new BasicDBObject();
    condition.put(UserBO.USERNAME_FIELD_NAME, user.getUsername());
    if (user.isExistingRecord()) {
        condition.put(BO.DOCUMENT_ID_FIELD_NAME, new BasicDBObject("$ne", user.getDocumentID()));
    }
    long i = coll.getCount(condition);
    if (i > 0) {
        throw new ChiliLogException(Strings.USER_DUPLICATE_USERNAME_ERROR, user.getUsername());
    }

    // Validate unique email address
    if (!StringUtils.isBlank(user.getEmailAddress())) {
        condition = new BasicDBObject();
        condition.put(UserBO.EMAIL_ADDRESS_FIELD_NAME, user.getEmailAddress());
        if (user.isExistingRecord()) {
            condition.put(BO.DOCUMENT_ID_FIELD_NAME, new BasicDBObject("$ne", user.getDocumentID()));
        }
        i = coll.getCount(condition);
        if (i > 0) {
            throw new ChiliLogException(Strings.USER_DUPLICATE_EMAIL_ADDRESS_ERROR, user.getEmailAddress());
        }
    }

    // Save it
    super.save(db, user);
}