How to update a object in mongodb via mongoose?

后端 未结 2 1851
清歌不尽
清歌不尽 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:14

    Now you can update directly .

    OrganizationModel.update(
     {name: 'Koka'}, 
     {'address.street': 'new street name'}, 
     callback);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题