-2

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' } ???
Chococo35
  • 79
  • 8

1 Answers1

1

use empty {} in query.

app.post('/api/persons',function(req,res){
    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.")
        }
    });
})
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
  • hey, thanks for the answer, but I get : The request didn't work:MongoError: >1 field in obj: {}. – Chococo35 May 24 '17 at 13:01
  • i need to view req.body, can you post here? – KARTHIKEYAN.A May 24 '17 at 13:29
  • yes, actually, I changed my values and the req.body is `{ evaluation: {}, exams: {}, setup: {}, physiological_data: { age: 5, gender: 'M' } }` In my DB, the value I want to find is exactly like that but the fields of evaluation, exams and setup are not empty – Chococo35 May 24 '17 at 13:40
  • ok, now the query works if I manage to send { physiological_data: { age: 5, gender: 'M' } } I get everything. But why if I try to send { evaluation:{S1:"present"},physiological_data: { age: 5, gender: 'M' }} it doesn't work even if there is one in the DB? It seems it doesn't work if I forget one field in one bracket. For example if I just do { physiological_data: { age: 5 } } it won't show me anything – Chococo35 May 24 '17 at 13:48
  • I guess the problem is that the value is nested. Is it possible to automatically change `{ physiological_data: { age: 5, gender: 'M' } }` to `{ 'physiological_data.age':5, 'physiological_data.gender':'M' }` ??? – Chococo35 May 24 '17 at 14:52
  • I tried to stringify the thing, but the query selector must be an object .... The stringification works though and gives a string with the perfect request. – Chococo35 May 24 '17 at 15:59