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(@Nullable final DBObject query) 

Source Link

Document

Same as #getCount(DBObject)

Usage

From source file:org.forgerock.openidm.repo.mongodb.impl.DBHelper.java

License:Open Source License

private static void populateDefaultUser(DBCollection collection, JsonValue completeConfig, String user,
        String pwd, String roles) {

    DBObject query = (DBObject) JSON.parse("{\"_openidm_id\": \"" + user + "\"}");
    if (collection.count(query) > 0) {
        return;/*from   w  w  w.ja  v a 2 s  .co  m*/
    }
    Map<String, Object> defAdmin = new LinkedHashMap<String, Object>();
    defAdmin.put("_id", user);
    defAdmin.put("_openidm_id", user);
    defAdmin.put("userName", user);
    try {
        defAdmin.put("password", JSON.parse(pwd));
    } catch (com.mongodb.util.JSONParseException e) {
        defAdmin.put("password", pwd);
    }
    String[] role = roles.split(",");
    defAdmin.put("roles", role);
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(defAdmin);

    DBObject o = builder.get();
    collection.insert(o);
}

From source file:org.hibernate.ogm.datastore.mongodb.MongoDBDialect.java

License:LGPL

private ClosableIterator<Tuple> doCount(MongoDBQueryDescriptor query, DBCollection collection) {
    long count = collection.count(query.getCriteria());
    MapTupleSnapshot snapshot = new MapTupleSnapshot(Collections.<String, Object>singletonMap("n", count));
    return CollectionHelper.newClosableIterator(Collections.singletonList(new Tuple(snapshot)));
}

From source file:org.s1.mongodb.log.MongoDBLogStorage.java

License:Apache License

@Override
public long list(List<Map<String, Object>> list, Map<String, Object> search, int skip, int max) {
    //return super.list(list, search, skip, max);
    DBCollection coll = getCollection();
    DBObject s = new BasicDBObject();
    if (search != null) {
        //remove $where
        search = Objects.iterate(search, new Closure<ObjectIterator.IterateBean, Object>() {
            @Override//from   w ww .j a va 2s .co m
            public Object call(ObjectIterator.IterateBean input) {
                if (input.getValue() instanceof Map) {
                    if (((Map) input.getValue()).containsKey("$where"))
                        ((Map) input.getValue()).remove("$where");
                }
                return input.getValue();
            }
        });
        s = new BasicDBObject(search);
    }
    DBCursor cur = coll.find(s).sort(new BasicDBObject("date", -1)).limit(max).skip(skip);

    while (cur.hasNext()) {
        Map<String, Object> m = cur.next().toMap();
        m.remove("_id");
        list.add(m);
    }

    return coll.count(s);
}

From source file:org.s1.mongodb.MongoDBQueryHelper.java

License:Apache License

/**
 *
 * @param c/*from  ww w  .ja  v a  2s .  com*/
 * @param search
 * @throws AlreadyExistsException
 */
public static void ensureNotExists(CollectionId c, Map<String, Object> search) throws AlreadyExistsException {
    DBCollection coll = MongoDBConnectionHelper.getConnection(c.getDatabase()).getCollection(c.getCollection());
    if (search == null)
        search = Objects.newHashMap();
    long cnt = coll.count(MongoDBFormat.fromMap(search));
    if (cnt > 0) {
        if (LOG.isDebugEnabled())
            LOG.debug("Record already exists: " + c + ", search: " + search);
        throw new AlreadyExistsException("MongoDB " + c + ", search: " + search);
    }
}

From source file:org.s1.mongodb.MongoDBQueryHelper.java

License:Apache License

/**
 *
 * @param c/*from w  w w. j  ava 2  s.  c  o  m*/
 * @param search
 * @throws NotFoundException
 * @throws MoreThanOneFoundException
 */
public static void ensureOnlyOne(CollectionId c, Map<String, Object> search)
        throws NotFoundException, MoreThanOneFoundException {
    DBCollection coll = MongoDBConnectionHelper.getConnection(c.getDatabase()).getCollection(c.getCollection());
    if (search == null)
        search = Objects.newHashMap();
    long cnt = coll.count(MongoDBFormat.fromMap(search));
    if (cnt == 0) {
        if (LOG.isDebugEnabled())
            LOG.debug("Record not found in " + c + ", search: " + search);
        throw new NotFoundException("MongoDB " + c + ", search: " + search);
    }
    if (cnt > 1) {
        if (LOG.isDebugEnabled())
            LOG.debug("More than one record found (" + cnt + ") found " + c + ", search: " + search);
        throw new MoreThanOneFoundException("MongoDB " + c + ", search: " + search);
    }
}

From source file:org.slc.sli.dal.repository.MongoRepository.java

License:Apache License

private long count(String collectionName, DBObject queryObject) {
    DBCollection collection = getCollection(collectionName);
    if (collection == null) {
        return 0;
    }/*from  ww w .  j  av a  2  s  .co m*/
    return collection.count(queryObject);
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

public long count(Query query, Class<?> entityClass, String collectionName) {

    Assert.hasText(collectionName);//from www  .  j a  v  a 2  s.  co  m
    final DBObject dbObject = query == null ? null
            : queryMapper.getMappedObject(query.getQueryObject(),
                    entityClass == null ? null : mappingContext.getPersistentEntity(entityClass));

    return execute(collectionName, new CollectionCallback<Long>() {
        public Long doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            return collection.count(dbObject);
        }
    });
}

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

License:Apache License

public static void assertPullStudy(DBCollection variantsCollection, String studyId, long expectedFileCount,
        long expectedStatsCount) {
    BasicDBObject variantFiles = new BasicDBObject(FILES_STUDY_ID_FIELD, studyId);
    BasicDBObject variantStats = new BasicDBObject(STATS_STUDY_ID_FIELD, studyId);

    assertEquals(expectedFileCount, variantsCollection.count(variantFiles));
    assertEquals(expectedStatsCount, variantsCollection.count(variantStats));
}