【问题标题】:collection.save() not persisting data to mongodbcollection.save() 没有将数据持久化到 mongodb
【发布时间】:2017-07-08 21:07:24
【问题描述】:

我对 Nodejs 完全陌生,我一直在尝试更新我的集合中的一个字段,但无济于事。这是我推入模型数组的代码。 update array field in model

然后这是我的架构。 course schema

更改未保存到我的数据库的任何原因?谢谢 。

注意:我尝试过使用markModified('enrolled_courses.completed_lesson'),但没有任何区别。

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongoose-schema


    【解决方案1】:
    router.route('/yourRoute').post((req, res) => {
    
      Schema.findOneAndUpdate({
        username: newName
      }, {
        $set: {
          enrolled_courses: [
            {
              key: newValue
    
            }
          ]
        }
      }, {
        upsert: true
      }, function(err, user) {
        if (err) {
          res.json(err);
        } else {
          res.send({message: "Successfully updated"});
    
        }
      })
    
    });
    

    【讨论】:

    • 您好,感谢您的回复,但是您的代码没有指定我们要在登记的课程数组中编辑的对象,而且它也没有推入完成的课程数组。请问还有什么想法吗?
    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多