如何在mongoose模式中添加哪些属性缺失但存在?

时间:2018-02-16 05:23:22

标签: mongodb mongoose mongoose-schema

我需要保存此文档,但是mongoose Schema包含对象中不存在的字段。如果客户端使用旧API,我想支持这两种变体。

   {
      "user": "someUser",
      "password": "qwerty",
   }

   const userSchema = new Schema({
          user: {
            type: String,
            required: true
          },
          password: {
            type: String,
            required: true
          },
         preferences: {
           banned: {
            type: Boolean,
            default: false
         }
        }
   })


   User.findOneAndUpdate({
        user: req.body.user
    }, {
        password: req.body.password
        preferences: {
            banned: true
        }
    }, {
        upsert: true,
        new: true,
        setDefaultsOnInsert: true
    }

0 个答案:

没有答案