Reputation: 220
I'm trying to use mongoose to query a collection by some field and its getting 0 results always.
This is the query
Users.find({"role":3}).exec((err,users) => {
if(err){
logger.error(properties.get("500.logging.getUsers"));
logger.error(err);
res.status(500).send({message:properties.get("http.response.500")});
}else{
if(!users){
res.status(404).send({message:properties.get("users.response.404")});
}else{
res.status(200).send({users:users});
}
}
});
And querying by mongo compass Im getting results
I also tried to put the role and 3 without quotes without success
Any suggestions?
Thanks in advise
Upvotes: 0
Views: 501
Reputation: 312169
Mongoose will cast your query values based on the type of each field as defined in the model's schema. So because you're defining role
as a string in your schema, the 3
in your query gets cast to '3'
and the documents with role: 3
aren't matched.
Upvotes: 1