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

标签 javascript node.js mongodb

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

关于javascript - 如何使用node.js在mongodb的嵌套数组中添加新对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35823457/

相关文章:

javascript - 在 angularjs 中使用地理定位

node.js - MongoDB-Memory-Server 使 jest 测试调试卡在 WSL 上

node.js - NextJS/Vercel/MongoDB FetchError : request to http://localhost:3000/api/gear failed, 原因: connect ECONNREFUSED 127. 0.0.1:3000

javascript - 如何获取 JS 中 YYYY-MM-DD 格式的第二天日期?

javascript - Knockout.JS 中的值绑定(bind)

node.js - 使用 npm list 时 'invalid' 是什么意思?

node.js - ExpressJS - Elastic Beanstalk 502 错误网关

javascript - 如何从匿名函数中传递值

MongoDB按数组内部元素分组

javascript - Rails 日期选择器 ActiveRecord 查找不起作用