These are chat archives for Automattic/mongoose

19th
Jan 2018
Yogendra Rawal
@rawalyogendra
Jan 19 2018 16:06
hello everyone!!
Yogendra Rawal
@rawalyogendra
Jan 19 2018 16:14
I can not update embedded discriminator objects with findOneAndUpdate (if this makes any sense). My code works when I try to update base schema.
    Event.findOneAndUpdate({_id: eventId}, {$set: req.body}, {new: true, runValidators: true}, function(err, event){
      if(err) {
        next(err);
      }
     //send response
    });
Yogendra Rawal
@rawalyogendra
Jan 19 2018 16:24
const eventSchema = new Schema({
  title: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  },
  eventType: {
    type: String,
    required: true,
  }
}, { discriminatorKey: 'eventType' });

const Event = mongoose.model('event', eventSchema);

const sportEvent = Event.discriminator("sports", new Schema({
  sportEvent: {
    sportName: {
      type: String,
      required: true
    },
    firstTeam: {
      type: String,
      required: true
    },
    secondTeam: {
      type: String,
      required: true
    },
  }
}));
here is my event model
when I update event name or description it works
but when I try to update firstTeam or secondTeam. It does not work
Kev
@lineus
Jan 19 2018 17:27
@rawalyogendra can you paste an example of what is being stored in mongo? I have a theory, but I don't want to throw it out there until I see what's being stored in the db.
Kev
@lineus
Jan 19 2018 18:30
is the Event.findOneAndUpdate that you pasted above the actual call that's failing for you? Or is that an example of one that's working?
Yogendra Rawal
@rawalyogendra
Jan 19 2018 23:40
yeah that is the actual call @lineus. It works when i try to update the data of eventSchema but it fails when i try to update sportEventwhich is a discriminator schema.
I am really new to mongodb or NoSQL in general