【问题标题】:need to update values inside an object using mongoose需要使用 mongoose 更新对象内部的值
【发布时间】:2017-06-24 09:07:21
【问题描述】:

这是我的模型。这里我从服务器接收一个名为响应的对象。现在我需要用这个响应对象更新数据库。 但问题是我只能更新变量而不是像父亲这样的对象。 父亲是一个对象,现在我需要更新父亲的名字。但是如果我使用Father.Firstname 说意外的标记,它会给我错误。 请帮我解决这个问题..

var User = mongoose.model('User', userSchema);

function createStudent(response) {
  console.log(response);
    var list = new User({
        Firstname : response.Fname,
        Age : response.age,
        Lastname : response.Lname,
        Father.Firstname : response.fatherfname,
        Father.Lastname : response.fatherlname,
        Father.Occupation : response.occupation,
        Father.PlaceOfWork : response.placeofwork,
        Father.OfficialAddress : response.officaladd,
        Father.EmailId : response.emailid,
        Father.PhoneNo : response.phoneno,
        Father.MobileNo : response.mobileno,


    });
    list.save();
  }

【问题讨论】:

    标签: javascript node.js mongodb reactjs mongoose


    【解决方案1】:

    如果您需要在对象键名中使用 dot(.) 字符,那么您需要用 double("")single('') 引号将其括起来,就像使用字符串一样。

    所以这样写你的代码 -

    "Father.Firstname" : response.fatherfname,
    

    而不是

    Father.Firstname : response.fatherfname,
    

    【讨论】:

    • 是的,一开始我认为它不会起作用。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2015-02-22
    • 2021-01-21
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    相关资源
    最近更新 更多