Wednesday, 12 April 2017

Mongoose populate an array of sub documents then set/update a field to it after

I am trying to assign data to a property of one my schemas after it has been populated, but am not sure how. I have a User schema which contains an array of my adminTeams model. In my adminTeams model, I am trying to set or update the adminAddress property after doing a populate.

//Partial User Schema

adminTeams: [{
    type: Schema.Types.ObjectId,
    ref: "Team"
  }],

//Teams Schema

var TeamSchema = new Schema({


  teamname: {
    type: String,
    trim: true,
    required: true
  },
  teamAdmin: {
    type: String
  },
  adminAvatar: {
    type: String
  },
  adminAddress: {
    type: String
  teamMembers: [{
    type: Schema.Types.ObjectId,
    ref: "User"
  }]


});

How can I update the adminAddress property after doing

User.findById({ "_id": req.body.userID }).populate('adminTeams')



via henhen

No comments:

Post a Comment