Java Code Examples for com.mongodb.DBCollection#count()

The following examples show how to use com.mongodb.DBCollection#count() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Find all DBObjects from database using this query. 
 * Note 1: it may do a full table scan if the query contains no index keys.
 * Note 2: it will fetch all content into JVM memory rather than use lazy loading.
 * So make sure you call it only at small collection such as configuration data.
 * 
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final List<DBObject> queryAllFromMongo(DBObject query, 
		String databaseName, String namespace, String collection, 
		DBObject fields, DBObject sortFields) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	int count = (int)coll.count(query);
	ArrayList<DBObject> objList = new ArrayList<DBObject>();
	DBCursor list = coll.find(query, fields);
	if ( sortFields != null ) {
		list = list.sort(sortFields);
	}
	while ( list.hasNext() ) {
		objList.add(list.next());
	}
	return objList;
}
 
Example 2
Source File: MongoUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Find all DBObjects from database using this query. 
 * Note 1: it may do a full table scan if the query contains no index keys.
 * Note 2: it will fetch all content into JVM memory rather than use lazy loading.
 * So make sure you call it only at small collection such as configuration data.
 * 
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final List<DBObject> queryAllFromMongo(DBObject query, 
		String databaseName, String namespace, String collection, 
		DBObject fields, DBObject sortFields, int numToSkip, int limit) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	int count = (int)coll.count(query);
	ArrayList<DBObject> objList = new ArrayList<DBObject>();
	DBCursor list = coll.find(query, fields).skip(numToSkip).limit(limit);
	if ( sortFields != null ) {
		list = list.sort(sortFields);
	}
	while ( list.hasNext() ) {
		objList.add(list.next());
	}
	return objList;
}
 
Example 3
Source File: MongoDBUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Find all DBObjects from database using this query. 
 * Note 1: it may do a full table scan if the query contains no index keys.
 * Note 2: it will fetch all content into JVM memory rather than use lazy loading.
 * So make sure you call it only at small collection such as configuration data.
 * 
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final List<DBObject> queryAllFromMongo(DBObject query, 
		String databaseName, String namespace, String collection, 
		DBObject fields, DBObject sortFields, int numToSkip, int limit) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	int count = (int)coll.count(query);
	ArrayList<DBObject> objList = new ArrayList<DBObject>();
	DBCursor list = coll.find(query, fields).skip(numToSkip).limit(limit);
	if ( sortFields != null ) {
		list = list.sort(sortFields);
	}
	while ( list.hasNext() ) {
		objList.add(list.next());
	}
	return objList;
}
 
Example 4
Source File: SelectVo.java    From tangyuan2 with GNU General Public License v3.0 5 votes vote down vote up
private long getQueryCount(DBCollection collection) {
	DBObject query = getQuery();
	if (null == query) {
		return collection.count();
	} else {
		return collection.count(query);
	}
}
 
Example 5
Source File: RootController.java    From hcp-cloud-foundry-tutorials with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody Result onRootAccess() {

    DBCollection collection = mongoTemplate.getCollection("test");
    long count = collection.getCount();
    log.info("Object count in 'test' collection before insert: " + count + "<br/> Inserting one object.<br/>");

    BasicDBObject dBObject = new BasicDBObject();
    dBObject.put("hello", "world");
    collection.insert(dBObject);
    count = collection.count();
    log.info("Object count in test collection after insert:" + count);

    Result result = new Result();
    List<DBObject> dbObjects = new ArrayList<DBObject>();
    DBCursor cursor = collection.find();
    while (cursor.hasNext()) {
        com.mongodb.DBObject obj = cursor.next();
        final String value = (String) obj.get("hello");
        DBObject object = new DBObject();
        object.setKey("hello");
        object.setValue(value);
        dbObjects.add(object);
    }
    result.setDbObjects(dbObjects);
    result.setStatus(
            "Successfully accessed Mongodb service. Retrieving the data object inserted in test collection.");
    collection.drop();
    return result;
}
 
Example 6
Source File: MongoDBUtil.java    From gameserver with Apache License 2.0 5 votes vote down vote up
/**
 * Count the result number for a query
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final long countQueryResult(DBObject query, String databaseName,
		String namespace, String collection) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	if ( coll != null ) {
		return coll.count(query);
	} else {
		return 0;
	}
}
 
Example 7
Source File: MongoRepository.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private long count(String collectionName, DBObject queryObject) {
    DBCollection collection = getCollection(collectionName);
    if (collection == null) {
        return 0;
    }
    return collection.count(queryObject);
}
 
Example 8
Source File: MongoUtil.java    From gameserver with Apache License 2.0 3 votes vote down vote up
/**
 * Count the result number for a query
 * @param query
 * @param databaseName
 * @param namespace
 * @param collection
 * @param fields
 * @return
 */
public static final long countQueryResult(DBObject query, String databaseName,
		String namespace, String collection) {
	
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	return coll.count(query);
}