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 }.
If I search for a 22 Y.O. person the object sended is
Object {age:22}
How can I get all the related documents to the query?
EDIT: these datas are not the one I use anymore, please see under
EDIT2: I guess the problem is that the value sent in the req.body is nested. Is it possible to automatically change
{ physiological_data: { age: 5, gender: 'M' } }
to
{ 'physiological_data.age':5, 'physiological_data.gender':'M' } ???