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:
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.