hibernate ogm mongo db, how to get count of collection?

870 views Asked by At

with mongo java driver for getting collection count, i use following method

 public Integer getUsersCount() {
        Integer listCount = 0;
        try {
            BasicDBObject query = new BasicDBObject();
            DBCursor cursor = mongoCoreService.getUserCollection().find(query);
            listCount = cursor.count();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listCount;
    }

Now I try do the same with hibernate ogm, but I cant find example or implementation. I have entity manager

public int getProductCount() {
        EntityManager em = getEntityManager();
        try {

        } finally {
            //em.close();
        }
         return 10;
    }

I tried with criteria API, but version what I use and which is work with Spring does not support criteria API. How can get collection count?

1

There are 1 answers

0
Gunnar On BEST ANSWER

You can run the following native query for getting the collection size:

long count = em.createNativeQuery( "db.users.count({})" ).getSingleResult();