0

I have this dataset and I want find and update by first finding by "_id" then "Questions" by _id using mongoose I tried this but it didn't work

Db.find({_id:id},function(err,data){
        data.Questions.find({_id:sub_id},function(err,dt){
        res.send(dt)
    })
    })

this is the dataset

[{
        "_id": "443",
        "name": "john",
        "Questions": [
           {
             "_id": "21",
             "question": "question 1",
                "options": [
                    {
                        "option": "yes"
                    },
                    {
                        "option": "no"
                    }
                ]
            },
            {
             "_id": "65",
             "question": "question 2",
                "options": [
                    {
                        "option": "small"
                    },
                    {
                        "option": "large"
                    }
                ]
            },
        ]
    }

For example if I want to search by "_id" = 443 and then find Questions where "_id" 65

sal
  • 1
  • 2
  • I'm not going to flag as duplicate, but I'll let you decide if any of these answers your question. [1](https://stackoverflow.com/questions/34431435/mongodb-update-an-object-in-nested-array) [2](https://stackoverflow.com/questions/48570436/updated-mongoose-update-nested-array-element) [3](https://stackoverflow.com/questions/20474697/updating-objects-within-nested-array-in-mongodb-with-multiple-criteria) [4](https://stackoverflow.com/questions/10522347/mongodb-update-objects-in-a-documents-array-nested-updating) – thammada.ts Jun 01 '20 at 18:44
  • Does this answer your question? [Updating objects within Nested Array in mongodb with multiple criteria](https://stackoverflow.com/questions/20474697/updating-objects-within-nested-array-in-mongodb-with-multiple-criteria) – Eric Jin Jun 01 '20 at 18:49
  • my dataset has a lot of entries and each one has id and Questions array, so i want to search by entry id and when i find the entry, then find question by id – sal Jun 01 '20 at 18:51
  • `data.Questions` is just a regular array, you use `.find()` with as a regular javascript array method, not mongoose `Model.find()` query. If want to update the data, please take the time to review other answers suggested in the comments. Otherwise please explain a bit more what you need. It seems you just want to find the element. – thammada.ts Jun 01 '20 at 19:22

0 Answers0