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.
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}
)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With