How do I set default value of an integer in mongodb?

后端 未结 1 379
挽巷
挽巷 2020-12-31 04:17

I\'m new to mongodb and node.js. Let me know, how do I set default value after creating the user.

Here is my current code:

// CREATE USER
app.post(\"         


        
相关标签:
1条回答
  • 2020-12-31 05:03

    As you're using Mongoose, you can set the default as part of the Schema definition:

    var userSchema = new Schema({ 
        win: { type: Number, default: 0 }
    });
    

    The options are documented here. It's also cool that if you set the default to a function, it will execute when the Model is instantiated. For example, if it were: default: Date.now, it will call the Date.now() function when a model is created.

    0 讨论(0)
提交回复
热议问题