Mongoose Boolean default false

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

active: { type: Boolean, default: false }

But for some reason, the mongoose always sets the field in true.

What can I do to change this?

+4
source share
1 answer

It seems that you are missing something. setting default: falsefor this field will automatically set to false.

const mongoose = require('mongoose');

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

mongoose.model('Project', projectSchema, 'project');
+6
source

Source: https://habr.com/ru/post/1618149/


All Articles