Wednesday 24 May 2017

MongoJS find is too specific

I am trying to query my mongoDB database in my MEAN Stack web application by sending in a POST request with the values I want to find in the database.

Here is what I'm doing :

app.post('/api/persons',function(req,res){
    console.log("I received a POST request on the DB. Querying :");
    console.log(req.body);
    db.persons.find(req.body,function(err,doc){
        if(err){
            console.log("The request didn't work:"+err+".");
        }else{
            res.json(doc);
            console.log("The request was successful.")
        }
    });
})

The code works, but only if I enter the exact value.

For example, if I search for a person that is 22 years old, I will find only the document { "age": 22 } and not the document { "name": "Catarina", "age": 22 }.

How can I get all the related documents to the query?



via Chococo35

No comments:

Post a Comment