Mongoose upsert没有为Schema.types.mixed属性设置默认值,setDefaultsOnInsert为true

时间:2017-06-02 19:23:51

标签: node.js mongoose schema upsert

我正在使用upsert函数来使用我的后端序列化用户。我使用的猫鼬的“findOneAndUpdate”与“UPSERT:真正的”和“setDefaultsOnInsert:真正的”集,让所有默认值在我的模式定义应该被用于那些我不传递给UPSERT所有参数

这是我的问题:

let serialize = (params, cb) => {

  User.findOneAndUpdate({"fbId" : params.fbId}, params, { upsert: true, setDefaultsOnInsert: true, new: true }, (err, doc) => {

    if (err) {
      //winston.log
      return cb (ErrorTypes.serverError(), null);
    }
    else if (doc) {
      return cb(null, doc);
    }
    else {
      return cb(ErrorTypes.serverError(), null);
    }

  });

}

对于此架构:

const userSchema = new Mongoose.Schema({

  fbId: {
    type: String,
    index: true,
    required: "Users must have a facebook ID.",
    maxlength: 40
  },
  firstName: {
    type: String,
    required: "Users must have a name.",
    maxlength: 40
  },
  lastName: {
    type: String,
    required: "Users must have a name.",
    maxlength: 40
  },
  gender: {
    type: String,
    required: "Users must have a gender",
    default: "Unknown"
  },
  ageRange: {
    type: String,
    default: "Unknown",
    maxlength: 25
  },
  pictureUrl: {
    type: String,
    required: "Users must have a profile picture."
  },
  dateJoined: {
    type: Number,
    required: "Users must have a joined date.", 
    default: date.getTime()
  },
  pushId: String,
  bookmarks: [{
    item: {
      type: Mongoose.Schema.Types.ObjectId,
      ref: 'Item',
      required: "An item id is required to create a bookmark."
    },
    lastOfferMade: Number,
    timeOfferMade: Number,
  }], default: [], 
  offersInCategory: {
      type: Object,
      validate: object => { 
        let allowedKeys = ['Furniture', 'Household', 'Electronics', 'Other'];
        let correctKeys = Object.keys(object).every(key => allowedKeys.includes(key)); 

        let min = 0;
        let correctValues = Object.values(object).every(value => value >= min); 

        return correctKeys && correctValues;
  }
  }, default: {
    'Furniture': 0, 
    'Household': 0, 
    'Electronics': 0,
    'Other': 0
  },
  rejectionsInCategory: {
      type: Object,
      validate: object => { 
        let allowedKeys = ['Furniture', 'Household', 'Electronics', 'Other'];
        let correctKeys = Object.keys(object).every(key => allowedKeys.includes(key)); 

        let min = 0;
        let correctValues = Object.values(object).every(value => value >= min ); 

        return correctKeys && correctValues; 
      }
  }, default: {
    'Furniture': 0, 
    'Household': 0, 
    'Electronics': 0,
    'Other': 0
  }, 
  blockedUsers: [{
    userId: String,
    blockedOn: Number
  }], default: [],
  blockedBy: [{
    userId: String,
    blockedOn: Number
  }], default: [],
  hasSeenWalkThrough: { type: Boolean, default: false },
  hasBookmarkedItem: { type: Boolean, default: false },
  hasSeenSearchPrompt: { type: Boolean, default: false }

});

传递这些参数:

var upsertNewParams = {
    fbId: "newUpsertFbId",
    firstName: "New upsert - User", 
    lastName: "Test Last Name - User", 
    gender: "Male", 
    ageRange: "18-22",
    pictureUrl: "url", 
    pushId: "12345"
}

我的upsert函数返回此文档:

{ _id: 5931b0efbeadf32d858a7578,
  fbId: 'newUpsertFbId',
  __v: 0,
  firstName: 'New upsert - User',
  lastName: 'Test Last Name - User',
  pictureUrl: 'url',
  pushId: '12345',
  hasSeenSearchPrompt: false,
  hasBookmarkedItem: false,
  hasSeenWalkThrough: false,
  blockedBy: [],
  blockedUsers: [],
  default: [],
  bookmarks: [],
  dateJoined: 1496429065565,
  ageRange: '18-22',
  gender: 'Male' }

我可以清楚地看到所有其他默认设置,例如“hasSeenWalkthrough”和“blockedBy / blockedUsers / bookmarks”设置,但我的“offersInCategory”和“rejectionsInCategory”由于某种原因没有,但我提供了这些属性的默认值和其他人一样。如何使用upsert设置这些默认值?

0 个答案:

没有答案