Default Value in MongoDB using Node.js Last Updated : 08 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Setting default values in MongoDB using Node.js can streamline the database management process by ensuring that certain fields always have an initial value even if they are not provided by the user. This practice is particularly useful for maintaining data integrity, setting fallback values, and simplifying the data insertion process. This guide will walk you through how to set default values in MongoDB when using Node.js, focusing on both the Mongoose ODM (Object Data Modeling) library and MongoDB’s native driver.Why Set Default Values?Data Integrity: Ensure that essential fields always have a value.Simplify Code: Reduce the need for manual default setting in your application logic.Consistency: Maintain consistent data structures across your database.Using MongooseMongoose is a popular ODM library for MongoDB that provides a straightforward way to define schemas, models, and default values. Below, we’ll explore how to set up default values in Mongoose.Default ValueThis value is entered when no value is entered as a value of the field in the collection.Installation StepsStep 1: Make a folder structure for the project.mkdir GFGStep 2: Navigate to the project directorycd GFGStep 3: Initialize the NodeJs project inside the GFG folder.npm init -yStep 4: Install the required dependencies by the following command:npm install mongooseProject StructureThe updated dependencies in package.json file will look like:"dependencies": { "mongoose": "^8.4.4", }Running the server on Local IP: Data is the directory where MongoDB server is present.mongod --dbpath=data --bind_ip 127.0.0.1Example: Implementation to show the default value in MongoDB using NodeJs. JavaScript // app.js // Importing mongoose module const mongoose = require("mongoose"); // Database Address const url = "mongodb://localhost:27017/GFG"; // Connecting to database mongoose .connect(url) .then((ans) => { console.log("Connected Successful"); }) .catch((err) => { console.log("Error in the Connection"); }); // Schema class const Schema = mongoose.Schema; // Creating Structure of the collection const collection_structure = new Schema({ name: { type: String, // String type required: true, }, marks: { type: Number, // Number type default: 100, }, }); // Creating collection const collections = mongoose.model("GFG2", collection_structure); // Inserting one document collections .create({ // Inserting value of only one key name: "aayush", }) .then((ans) => { console.log(ans); }) .catch((err) => { console.log(err.message); }); Step to Run Application: Run the application using the following command from the root directory of the projectnode index.jsOutput: Console output- Default Value is inserted. Comment More infoAdvertise with us Next Article Default Value in MongoDB using Node.js zack_aayush Follow Improve Article Tags : Technical Scripter Web Technologies Node.js MongoDB Technical Scripter 2020 NodeJS-Questions +2 More Similar Reads How to set the document value type in MongoDB using Node.js? Mongoose.module is one of the most powerful external module of the node.js. Mongoose is a MongoDB ODM i.e (Object database Modelling) that used to translate the code and its representation from MongoDB to the Node.js server. Mongoose module provides several functions in order to manipulate the docum 2 min read How to Create Indexes in MongoDB using Node.js? MongoDB, a popular NoSQL database, provides powerful indexing capabilities to improve query performance. Indexes in MongoDB help in quickly locating documents and speeding up read operations. In this tutorial, we'll explore how to create indexes in MongoDB using Node.js. What is an Index in MongoDB? 3 min read How to Get Data from MongoDB using Node.js? One can create a simple Node.js application that allows us to get data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. Also, we use the EJS for our front end to render the simple HTML form and a table to show the data. Prerequisi 6 min read How to Use MongoDB Transactions in Node.js? Using MongoDB transactions in Node.js involves several steps. Transactions allow multiple operations on the database to be executed in an all-or-nothing manner, ensuring data consistency. we will learn how to use MongoDB transaction in Node.js.PrerequisitesMongoDB (Version 4.0 or higher Recommended) 2 min read How to Post Data in MongoDB Using NodeJS? In this tutorial, we will go through the process of creating a simple Node.js application that allows us to post data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple 5 min read How to drop collection in MongoDb using Node.js ? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo 2 min read How to drop database of MongoDB using Node.js ? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo 2 min read Sorting MongoDB Databases in Ascending Order (ASCII value) using Node.js MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo 2 min read MongoDB Required Constraint using Node.js Mongoose module is one of the most powerful external modules of the node.js.Mongoose is a MongoDB ODM i.e (Object database Modelling) that used to translate the code and its representation from MongoDB to the Node.js server. Mongoose module provides several functions in order to manipulate the docum 2 min read How to create new Collection in MongoDB using Node.js ? MongoDB the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This for 1 min read Like