18

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?

0

1 Answer 1

40

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');
Sign up to request clarification or add additional context in comments.

1 Comment

At first I thought "This is not a helpful answer, I am missing nothing!" And then I realized I was missing the type declaration, type: Boolean; thanks to this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.