Kageetai
Kageetai

Reputation: 1298

Mongoose Boolean default false

I have a mongoose schema with a boolean field which I want to have a default value of false. My first guess how to do that was like that:

active: { type: Boolean, default: false }

But for some reason mongoose is always setting the field to true.

What can I do to change that?

Upvotes: 18

Views: 41584

Answers (1)

Kunal Kapadia
Kunal Kapadia

Reputation: 3333

Looks like you are missing something. setting default: false for that field will auto set it to false.

const mongoose = require('mongoose');

const projectSchema = new mongoose.Schema({
    isUsed: {
        type: Boolean,
        default: false
    }
});

mongoose.model('Project', projectSchema, 'project');

Upvotes: 40

Related Questions