Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 965
Kageetai Avatar asked Dec 01 '15 17:12

Kageetai


1 Answers

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');
like image 134
Kunal Kapadia Avatar answered Sep 22 '22 01:09

Kunal Kapadia