I am using Hibernate OGM (5.2.0.Alpha1) with Mongodb (3.4)
I executed Mongodb Count Query Using Hibernate OGM by 2 below way
(1). Count query using Mongodb count query db.tablename.count({..})
(2). Count query by Mongodb Aggregate query...
Description
(1). Count query using Mongodb count query db.tablename.count({..})
public long executeCountQuery(){
//below line Works fine because value is 1.3 with $lt
//String query= "db.sales_stone_details.count({'$and':[{'size' : { '$lt' : 1.3}}]})"
//below line gots Error because value is 0.3(less than 1) with $lt
String query= "db.sales_stone_details.count({'$and':[{'size' : { '$lt' : 0.3}}]})"
long count = entityManager.createNativeQuery(query.toString()).getSingleResult();
return count;
}
(2). Count query by Mongodb Aggregate query...
public long executeCountQuery(){
//below line Works fine because value is 1.3 with $lt
//String query= "db.sales_stone_details.aggregate([{'$match': {'size': {'$lt': 1.3}}},{'$group': {'_id' : ObjectId(\"59df0d28f16b004af040c38c\"), 'countForAggregate' : {'$sum' : 1}}}])"
//below line gots Error because value is 0.3(less than 1) with $lt
String query= "db.sales_stone_details.aggregate([{'$match': {'size': {'$lt': 0.3}}},{'$group': {'_id' : ObjectId(\"59df0d28f16b004af040c38c\"), 'countForAggregate' : {'$sum' : 1}}}])"
List<Stone> stones = entityManager.createNativeQuery(query.toString() , Stone.class).getResultList();
long count = 0;
if (stones != null || !stones.isEmpty()){
count = stones.get(0).getCountForAggregate();
}
return count;
}
Error
org.bson.json.JsonParseException: JSON reader was expecting a value but found 'db'.
at org.bson.json.JsonReader.readBsonType(JsonReader.java:243) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:149) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.Document.parse(Document.java:105) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.Document.parse(Document.java:90) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.parseAsObject(MongoDBQueryDescriptorBuilder.java:252) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.parse(MongoDBQueryDescriptorBuilder.java:239) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:211) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:892) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:169) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:212) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.getCustomQuery(NativeNoSqlQueryInterpreter.java:50) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.createQueryPlan(NativeNoSqlQueryInterpreter.java:45) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:210) ~[hibernate-core-5.1.10.Final.jar:5.1.10.Final]
at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:301) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606) ~[hibernate-entitymanager-5.1.10.Final.jar:5.1.10.Final]
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529) ~[hibernate-entitymanager-5.1.10.Final.jar:5.1.10.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at com.sun.proxy.$Proxy7512.getSingleResult(Unknown Source) ~[na:na]
at com.techhive.sales.service.impl.GenericModelDaoImpl.executeCountQuery(GenericModelDaoImpl.java:621) ~[classes/:na]
...............
...........
I got same error with $gt , $lte , $gte
operators also.
But it seems like $gt , $gte , $lt , $lte
is not working with less than 1 value..
Is I am right???