Query with find operation is not working

I am trying the following query but hibernate ogm is giving wrong result.

db.mycollection.find({“cav_id” :3},{_id:0}).count()

mongodb shell output is
1

while hibernate ogm’s output
3

This is the result after executing this

entityManager.createNativeQuery(queryModel.getQuery()).getResultList();

where queryModel.getQuery() = db.mycollection.find({“cav_id” :3},{_id:0}).count()

Output :
[3.0, 24.0, 45000.0, Document{{House_No=103.0, Street_No=45.0, Block=F, Contact=Document{{first_cell=1.23458679E8, second_cell=9.87165243E8, third_cell=6.54912873E8}}}}]

hibernate is returning the object after find operation but not executing the count() operation.

This is the output from Mongo Shell

db.mycollection.find({“cav_id” :3},{_id:0});
{ “cav_id” : 3, “age” : 24, “salary” : 45000, “Address” : { “House_No” : 103, “Street_No” : 45, “Block” : “F”, “Contact” : { “first_cell” : 123458679, “second_cell” : 987165243, “third_cell” : 654912873 } } }

db.mycollection.find({“cav_id” :3},{_id:0}).count()
1

Hibernate OGM doesn’t map queries exactly like MongoDB, meaning that some of the syntax supported by MongoDB is not supported by OGM.

That said, If you rewrite the query as

db.mycollection.count({“cav_id” :3},{_id:0});

it should work.

You can find additional examples in the documentation: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#ogm-mongodb-queries-native

Also, for this case, you should use .getSingleResult() instead of .getResultList()
Let me know if it doesn’t work.

I think the reason is returning 3 in OGM is because it is truncating the .count() from the query when executing it. It’s returning the id instead of the count.

Cheers

Thanks , Now its working.