niren
niren

Reputation: 2731

Do not allow null in Mongodb

I am using mongoose. I want to create a field with not null as sql does means when a null value try to insert in a collection, it shouldn't allow to store null value.

Upvotes: 3

Views: 6138

Answers (2)

user14888519
user14888519

Reputation:

const mongoose = require('mongoose');

const Schemaname = new mongoose.Schema({
field1: {
        type: String,
        required: true
    },
field2: {
        type: Number,
        required: true
    }
});

module.exports = mongoose.model("Schemanames",Schemaname);

Upvotes: 2

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

You can use Mogoose validators to validate your model before saving it. There is no way for doing it at mongodb level. MongoDB is a NoSQL, schemaless database.

The required option might be useful

SchemaName = mongoose.Schema(
fieldName: {type: type, required: true}
)

Upvotes: 8

Related Questions