A K A K - 7 months ago 14
Javascript Question

Query list based on input list value

I stored data in mongoDB in User Collection in below format

{
"_id" : ObjectId("571ded1a1b8bd2c34aaf3c4a"),
"_class" : "com.unisys.workbench.domain.User",
"userName" : "admin",
"password" : "admin",
"email" : "xyzq@gmail.com",
"contact1" : "9889482416",
"contact2" : "1234567890",
"roles" : [
{
"roleName" : "ROLE_ADMIN"
}
]
}


To Restive data based on roleName I written Below Query

List<User> users= mongoTemplate.find(new Query().addCriteria(Criteria.where("roles.$roleName").is(role)),
User.class);


But its always returning null .

For email below Query is working fine

mongoTemplate.findOne(new Query().addCriteria(Criteria.where("email").is(email)),
User.class);

Answer

What is the $ for? It should be:

List<User> users= mongoTemplate.find(new Query().addCriteria(Criteria.where("roles.roleName").is(role)),
                User.class);
Comments