繁体   English   中英

如何使用node.js在mongodb的嵌套数组内添加新对象?

[英]How to add new objects inside nested array for mongodb using node.js?

我在mongoDB中存储了以下数据库结构:

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

我的问题是:如何将新对象推送到名为Mike的学生(其ID为“ B00123456”)中的图像数组中,我知道我应该使用mongoDB的update和set方法。 但我只是找不到实现该目标的方法。 我想要的结果是:

"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":""
        }
    ]
}
]

以下是我尝试使用MongoDB的更新和设置的内容:

    // 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);
         }
        );
    }
    });

有什么帮助吗?

update()函数是

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

第一个参数是selector ,请尝试这个

    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){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM