Database, Schema & Models | Mongoose - Everything you need to know

Database, Schema & Models | Mongoose - Everything you need to know

What is monngoDB?

MongoDB is a source-available, cross-platform NoSQL document database. It stores data in JSON-like formats, known as documents, which are grouped into collections.

MongoDB is a highly scalable database, offering both horizontal and vertical scaling to ensure availability across geographic regions. This makes it ideal for handling large, distributed datasets.

Being an unstructured database, MongoDB allows flexibility in document structure. You can have any number of fields or omit fields as needed, unlike SQL databases where fields must exist but can be left empty. While MongoDB is schema-less by default, you can define a fixed schema for certain fields if needed, allowing you to enforce constraints where necessary.

Adding and removing data in MongoDB is fast and simple. It’s designed to handle write-heavy operations, making it suitable for applications requiring frequent data writes. Each document is uniquely identified by an _id field, which ensures the uniqueness of data across the entire collection.

MongoDB is primarily used in real-time applications, big data processing, and distributed computing, where rapid data insertion and scalability are essential.


A schema in mongoDB is a model that describes the structure of a collection of documents, including the types of data each field can contain.

MongoDB uses a flexible schema that means you every document in a collection can have different number of field or a field in a document can represent different data type like number, string, array. We can makes some of the fields as required , which means whenever we create a new document there will be fields which needs to be populated or else we will get error. Like in user signup schema we need username, fullname, email, password as required.

These rules are called as Schema validation, it lets you create validation rules for your field, such as allowed data types and values ranges.

  • For a users collection, ensure that the?password?field is only stored as a string. This validation prevents users from saving their password as an unexpected data type, like an image.
  • For a sales collection, ensure that the?item?field belongs to a list of items that your store sells. This validation prevents a user from accidentally misspelling an item name when entering sales data.
  • For a students collection, ensure that the?gpa?field is always a positive number. This validation prevents errors during data entry.

Even if you try to updated or insert a document with incorrect datatype filed you will get invalid document error from mongoDB.

This flexible schema makes removing and adding of document fields very easy where as in DBMS we need to delete the whole table and copy its data and create new DB.


What is model in mongoDB?

In MongoDB, a model is a higher-level concept often used in conjunction with Mongoose, which is an Object Data Modeling (ODM) library for MongoDB and Node.js. A model represents the structure of documents within a collection and provides an interface that lets you interact with the database.

A model is created by combining a schema with a specific MongoDB collection. This model is then exported and used to interact with the database. For example, to insert documents, you create a new instance of that model class and then use .save() to persist the document to the database.

To query data from the collection, you use the model class to perform various operations like finding, adding, updating, or removing documents.

Steps to Create a Model:

  1. Define a Schema: Use MoWhat is monngoDB?

MongoDB is a source-available, cross-platform NoSQL document database. It stores data in JSON-like formats, known as documents, which are grouped into collections.

MongoDB is a highly scalable database, offering both horizontal and vertical scaling to ensure availability across geographic regions. This makes it ideal for handling large, distributed datasets.

Being an unstructured database, MongoDB allows flexibility in document structure. You can have any number of fields or omit fields as needed, unlike SQL databases where fields must exist but can be left empty. While MongoDB is schema-less by default, you can define a fixed schema for certain fields if needed, allowing you to enforce constraints where necessary.

Adding and removing data in MongoDB is fast and simple. It’s designed to handle write-heavy operations, making it suitable for applications requiring frequent data writes. Each document is uniquely identified by an _id field, which ensures the uniqueness of data across the entire collection.

MongoDB is primarily used in real-time applications, big data processing, and distributed computing, where rapid data insertion and scalability are essential.


A schema in mongoDB is a model that describes the structure of a collection of documents, including the types of data each field can contain.

MongoDB uses a flexible schema that means you every document in a collection can have different number of field or a field in a document can represent different data type like number, string, array. We can makes some of the fields as required , which means whenever we create a new document there will be fields which needs to be populated or else we will get error. Like in user signup schema we need username, fullname, email, password as required.

These rules are called as Schema validation, it lets you create validation rules for your field, such as allowed data types and values ranges.

  • For a users collection, ensure that the?password?field is only stored as a string. This validation prevents users from saving their password as an unexpected data type, like an image.
  • For a sales collection, ensure that the?item?field belongs to a list of items that your store sells. This validation prevents a user from accidentally misspelling an item name when entering sales data.
  • For a students collection, ensure that the?gpa?field is always a positive number. This validation prevents errors during data entry.

Even if you try to updated or insert a document with incorrect datatype filed you will get invalid document error from mongoDB.

This flexible schema makes removing and adding of document fields very easy where as in DBMS we need to delete the whole table and copy its data and create new DB.

要查看或添加评论,请登录

Mayur ?的更多文章

  • HOC in react

    HOC in react

    What is higher order components? A Higher-Order Component (HOC) is an advanced pattern in React that takes a component…

  • Authentication with JWT

    Authentication with JWT

    What is JWT? JWT stands for JSON Web Token, which is a compact, URL-safe means of securely representing data or claims…

  • What are advance routing techniuques?

    What are advance routing techniuques?

    The characters , , , and are subsets of their regular expression counterparts. The hyphen () and the dot () are…

  • What is express.js?

    What is express.js?

    Express.js is a free opensource, backend ,web application framework written in javascript used for building RESTful…

  • Let's learn about React's State and Hooks

    Let's learn about React's State and Hooks

    What is state? State is a built-in React object that stores data relevant to a component's current instance. Whenever…

  • Everything you want to know about module.exports/module/require.

    Everything you want to know about module.exports/module/require.

    A module in Node.js is a group of reusable JavaScript code that can be exported from one file and imported into another.

  • What is v8 architecture, and how does it works?

    What is v8 architecture, and how does it works?

    v8 architecture defines how v8 engine works. And what goes into when we perform any javascript task.

  • What is event loop(node.js)?

    What is event loop(node.js)?

    Whenever the call stack receives asynchronous operations such as , , API calls, or file system operations, Node.js…

  • What are pre/post middlewares in Mongoose?

    What are pre/post middlewares in Mongoose?

    In MongoDB, pre-hooks (or pre middleware) are functions in Mongoose that run before specific actions, like , , or , are…

社区洞察

其他会员也浏览了