【问题标题】:How to save object in mongoose如何在猫鼬中保存对象
【发布时间】:2018-10-18 22:22:16
【问题描述】:

我想使用对象来组合管理员,但我想不通。这是架构和代码

UserSchema = new mongoose.Schema({
  name: { type:String },
  owner : {
    Admin       : mongoose.Schema.Types.ObjectId
    }
});

及保存功能:

user.name = 'nerkn';
user.owner={ Admin     :new ObjectId(Adminid)} // this is from another user


el  = req.db.model('Users')(user)
el.save((err)=>{console.log(err)})
console.log('el', el)

当我检查名称已保存但 owner.Admin 未保存时。

【问题讨论】:

    标签: express mongoose mongoose-schema


    【解决方案1】:

    你应该在保存之前标记为修改的嵌套对象

    el.markModified('owner');
    

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 2022-10-07
      • 2014-07-26
      • 2018-07-12
      • 2017-04-27
      • 2021-01-30
      相关资源
      最近更新 更多