1

I have the following database structure stored in the mongoDB:

    "location" : "Halifax",
    "students" : [
    {
        "name": "Mike",
        "ID": "B00123456",
        "images": [
            {
                "image_url":"",
                "image_id":""
            }
        ]
    },
    {
        "name": "Rinan",
        "ID": "B00999999",
        "images": [
            {
                "image_url":"",
                "image_id":""
            }
        ]
    }
   ]

My question is: how do I push a new object to images array inside a student named Mike who has an ID of "B00123456", I know I should use mongoDB's update and set method. But I just couldn't find a way to achieve that. The result I want is:

"location" : "Halifax",
"students" : [
{
    "name": "Mike",
    "ID": "B00123456",
    "images": [
        {
            "image_url":"",
            "image_id":""
        },
        {
            "image_url":"www.example.com",
            "image_id":"uqxhqbxqx_1219"
        }
    ]
},
{
    "name": "Rinan",
    "ID": "B00999999",
    "images": [
        {
            "image_url":"",
            "image_id":""
        }
    ]
}
]

Below is what I am trying using MongoDB's update and set:

    // Connect and create a new doc
    MongoClient.connect('mongodb://username:password@iad1- mongos0.objectrocket.com:someNode/db_name', functionb(err, db) {
    if (err) {
        console.dir(err);
        console.log("error connected to mongodb");
    } else {
        var collection = db.collection('student_info_collection');
        var student_name = req.body.name;
        var student_id = req.body.ID;
        collection.update( 
                 { location:"Halifax" },
                 { ID:student_id}
                 { name: student_name},
                 {$push: { 
                            {
                                "images": [
                                    {
                                        "image_url":"www.example.com",
                                        "image_id":"uqxhqbxqx_1219"
                                    }
                             ]
                      } 
                 }
         }, function(err,result){
            if (err)
                console.log("Something's wrong");
            else
                res.sendStatus(200);
         }
        );
    }
    });

Any help?

1 Answer 1

2

The update() function is

 update(selector, document[, options][, callback])

The first parameter is selector, please try this one

    var student_name = req.body.name;
    var student_id = req.body.ID;
    collection.update( 
             { location:"Halifax", 
               'students.ID': student_id, 
               'students.name': student_name},
             {$push: { "students.$.images": 
                                {
                                    "image_url":"www.example.com",
                                    "image_id":"uqxhqbxqx_1219"
                                }
                     }
     }, function(err,result){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.