Spring data + Mongodb + query single value?

mongodb, spring

Solution

In method

find(Query query, Class<YourCollection> entityClass)

entityClass should be the corresponding collection, not the type of id.

If you are just trying to get id use

Query query = Query.query(Criteria.where("primaryEmail").is (email));    
query.fields().include("_id");
mongoTemplate.find(query, <YourCollection>.class).getId();

If you only include _id, all the other fields will be null in your result.

Problem

how to query a field instead of a whole object? I am trying to do something like that, want to see is that possible? ``` public BigInteger findUserIDWithRegisteredEmail(String email){ Query query = Query.query(Criteria.where("primaryEmail").is (email)); query.fields().include("_id"); return (BigInteger) mongoTemplate.find(query, BigInteger.class); } ```

Original source