Search code examples
javascriptnode.jsmongodbexpressmongoose

findOneAndUpdate Not Updating Discriminator


I am working on a REST API using Node, Express and Mongoose. Everything works perfectly when I update the base model. But when I try to update the discriminator object sportEvent in this case, it doesn't work.

Event.js - Event data model has a base schema common for all the collections with a discriminator for additional detail for that collection.

// base schema for all the events
// includes basic detail for all the events
const eventSchema = new Schema({
  //title for the event
  title: {
    type: String,
    required: true
  },

  //description for the events 
  description: {
    type: String,
    required: true
  },

  //event type for the event. such as Music, Sports, Expo, Leisure
  eventType: {
    type: String,
    required: true,
  }
}, { discriminatorKey: 'eventType' });

//sport event model for extending the basic event model
const sportEvent = Event.discriminator("sports", new Schema({
  sportEvent: {
    //sport name. for eg: cricket, football, etc
    sportName: {
      type: String,
      required: true
    },
    //first team name
    firstTeam: {
      type: String,
      required: true
    },
    //second team name
    secondTeam: {
      type: String,
      required: true
    },
  }
}));

EventController.js - has a PUT method for updating the collection. Here is a code snippet.

//for updating the event added a PUT method in /event route
router.put('/events/:eventId', function(req, res, next){
  //getting the event id form the url
  eventId = req.params.eventId;

  //checking the provided event id is a valid mongodb _id object or not
  if(objectId.isValid(eventId)){
    Event.findOneAndUpdate({_id: eventId}, {$set: req.body}, {new: true, runValidators: true}, function(err, event){
      if(err) {
        next(err);
      }
      sendResponse(res, "Event Successfully Updated", event);
    });
  } else {
    //sending a bad request error to the user if the event id is not valid
    sendError(res, 400, "Invalid Event ID");
  }
});

Solution

  • Ensure the discriminator key is present in the update object, or as an argument to the update function, write a switch case based on discriminator key, call update on the specific Schema type

    callback = function(err, doc){
        if(err) console.log(err)
        console.log(doc)
    };
    
    var id = ObjectId("5a75d22e6dabf3102c059f56");
    
    var update = {
        title : 'title-name',
        eventType : 'sports' ,
        sportEvent : {
            firstTeam : 'first-name',
            secondTeam : 'second-name',
            sportName : 'sport-name'
        }
    };
    
    switch(update.eventType){
        case 'sports':
            SportEventSchema.findByIdAndUpdate(id, {$set : update}, {new : true, upsert : false}, callback)
            break;
        case 'games':
            GameEventSchema.findByIdAndUpdate(id, {$set : update}, {new : true, upsert : false}, callback)
            break;
        default:
            Event.findByIdAndUpdate(id, {$set : update}, {new : true, upsert : false}, callback);
            break;
    }
    

    output : update for a sports event type

    Mongoose: events.findAndModify({ eventType: 'sports', _id: ObjectId("5a75d22e6dabf3102c059f56") }, [], { '$set': { title: 'title-name', eventType: 'sports', sportEvent: { firstTeam: 'first-name', secondTeam: 'second-name', sportName: 'sport-name' } } }, { new: true, upsert: false, remove: false, fields: {} })
    { sportEvent:
       { firstTeam: 'first-name',
         secondTeam: 'second-name',
         sportName: 'sport-name' },
      eventType: 'sports',
      _id: 5a75d22e6dabf3102c059f56,
      title: 'title-name',
      description: 'desc',
      __v: 0 }