Hictus
Hictus

Reputation: 220

Mongoose query not working as expected

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

enter image description here

I also tried to put the role and 3 without quotes without success

Any suggestions?

Thanks in advise

Upvotes: 0

Views: 501

Answers (1)

JohnnyHK
JohnnyHK

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

Related Questions