Revert to default value in mongoose if field is set to null

三世轮回 提交于 2019-12-11 03:36:22

问题


I am using mongoose with nodeJS. Consider the following schema:

var PersonSchema = new mongoose.Schema({
    "name": {type: String, default: "Human"},
    "age": {type: Number, defualt:20}
});
mongoose.model('Person', PersonSchema);

var order = new Order({
    name:null
});

This creates a new Person document with name set to null.

{
    name:null,
    age: 20
}

Is there anyway to check if the property being created/updated is null and if it is null set it back to default. The following declaration

var order = new Order();
order.name = null;
order.save(cb);

should create a new Person document with name set to default.

{
    name:"Human",
    age: 20
}

How would one implement this using NodeJs and mongoose.


回答1:


Well there are a few way to go about this:

PRE-SAVE/PRE-VALIDATE hooks

Mongoose Middleware hooks

Middleware (also called pre and post hooks) are functions which are passed control during execution of asynchronous functions.

PersonSchema.pre('save', function(next) {
    if (this.name === null) {
        this.name = 'Human';
    }

    next();
});

ENUM

Mongoose Validation and Mongoose Enums

Strings have enum, match, maxlength and minlength validators.

var PersonSchema = new mongoose.Schema({
    "name": {type: String, default: "Human", enum: ['Human', 'NONE-Human']},
    "age": {type: Number, defualt:20}
});

Update 1

If I have 20 fields where I want to check whether they are set to null, do I have to repeat this.field = default for all of them?

I guess you would have to.

What does NONE-Human in enums do? I could not find documentation for this online.

That is just an example of ENUM with enum you can only choose values that are specified in ENUM i.e. 'Name' can only have values of 'Human' or 'NONE-Human'.




回答2:


A little late to answer this, but a more generic approach would be not to hardcode the fields again in the pre hook.

Instead let's use the schema itself to go over the fields and check for the fields that have default values and are explicitly set to null. In this case we set it to the default value.

PersonSchema.pre('save', function(next) {
  const self = this;
  Object.keys(this.schema.paths).forEach(function(key) {
    if(self.schema.paths[key].options.default && self[key] === null) {
      self[key] = self.schema.paths[key].options.default;
    }
  });
  next();
});



回答3:


An even later answer. I was looking for a simpler solution and came across this question. Thought I'd share what I ended up doing.

db.Person.create({
   name: usersName || "Human",
   age: usersAge,
})


来源:https://stackoverflow.com/questions/38256182/revert-to-default-value-in-mongoose-if-field-is-set-to-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!