hibernate ogm mongo db,如何获取 collection 的计数?
hibernate ogm mongo db, how to get count of collection?
使用 mongo java 驱动程序获取 collection 计数,我使用以下方法
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;
}
现在我尝试对 hibernate ogm 做同样的事情,但我找不到示例或实现。
我有实体经理
public int getProductCount() {
EntityManager em = getEntityManager();
try {
} finally {
//em.close();
}
return 10;
}
我尝试使用标准 API,但是我使用的版本和与 Spring 一起使用的版本不支持标准 API。如何获得 collection 计数?
您可以运行以下本机查询来获取集合大小:
long count = em.createNativeQuery( "db.users.count({})" ).getSingleResult();
使用 mongo java 驱动程序获取 collection 计数,我使用以下方法
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;
}
现在我尝试对 hibernate ogm 做同样的事情,但我找不到示例或实现。 我有实体经理
public int getProductCount() {
EntityManager em = getEntityManager();
try {
} finally {
//em.close();
}
return 10;
}
我尝试使用标准 API,但是我使用的版本和与 Spring 一起使用的版本不支持标准 API。如何获得 collection 计数?
您可以运行以下本机查询来获取集合大小:
long count = em.createNativeQuery( "db.users.count({})" ).getSingleResult();