0

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?

Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56

1 Answers1

1

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

long count = em.createNativeQuery( "db.users.count({})" ).getSingleResult();
Gunnar
  • 18,095
  • 1
  • 53
  • 73