How to update a object in mongodb via mongoose?

后端 未结 2 1863
清歌不尽
清歌不尽 2021-02-12 15:34

I have mongoose schema as:

var Organization = new Schema({
  name: String,
  address: {
    street : String,
    city: String
  }
}, { collection: \'organization         


        
2条回答
  •  悲哀的现实
    2021-02-12 16:15

    I can't find any docs that cover this simple case so I can see why you're having trouble. But it's as simple as using a $set with a key that uses dot notation to reference the embedded field:

    OrganizationModel.update(
      {name: 'Koka'}, 
      {$set: {'address.street': 'new street name'}}, 
      callback);
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题