如何使用MERN堆栈仅发送要更新的字段?

时间:2020-02-28 20:10:27

标签: node.js reactjs mongodb express mern

例如,这是一个MongoDB模式:

  const humanBodySchema = new Schema({
       Head:{
           eyeColor: String,
           noseShape: String,
           lipShape: String,
           hair: Boolean,
       }
    },{
    timestamps:true,
  })

当我发送帖子或发出请求时,我将需要填写所有字段,如下所示:

{
   "Head":
     {
      "eyeColor": "Brown",
      "noseShape": "Pointy",
      "lipShape": "Puckered",
      "hair": "0"
     }
}

例如,如果我只想发送头发字段,该怎么办?

1 个答案:

答案 0 :(得分:0)

我知道了。我必须在字段上将type属性设置为“ Schema.Types.Mixed”。因此,humanBodySchema必须是这样的:

const humanBodySchema = new Schema({
       Head:{
           eyeColor: String,
           noseShape: String,
           lipShape: String,
           hair:{Schema.Types.Mixed},
       }
    },{
    timestamps:true,
  })