【问题标题】:findOneAndUpdate Not Updating DiscriminatorfindOneAndUpdate 不更新鉴别器
【发布时间】:2018-06-28 20:17:39
【问题描述】:

我正在使用 Node、Express 和 Mongoose 开发 REST API。当我更新基本模型时,一切正常。但是当我在这种情况下尝试更新鉴别器对象sportEvent时,它不起作用。

Event.js - 事件数据模型具有所有集合通用的基本架构,并带有用于该集合的更多详细信息的鉴别器。

// 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 - 具有用于更新集合的 PUT 方法。这是一个代码sn-p。

//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");
  }
});

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    确保鉴别键存在于更新对象中,或者作为更新函数的参数,根据鉴别键编写一个switch case,在特定的Schema类型上调用更新

    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;
    }
    

    输出:运动事件类型的更新

    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 }
    

    【讨论】:

    • 本来就是这样的。但出于显而易见的原因,我想从基本模式中更新它。它是一种解决方法。我最近在 mongoose 的 GitHub 上看到了一个类似功能的功能请求。希望他们实施。我将标记为我的答案。
    • @YogendraRawal 嗨,您是否有关于 Mongoose 的此功能请求的问题 ID?谢谢。
    • 如果有人在寻找问题:github.com/Automattic/mongoose/issues/6087
    猜你喜欢
    • 1970-01-01
    • 2019-05-21
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多