MongoDB is a NoSQL database that stores data in a flexible structure. A database is like a container for data, a collection is a group of related records, and a document is a single record stored in JSON-like format.
MongoDB can have many databases on one server, each separate from the others.
It creates three default databases: admin, local, and config (used internally).
A new database is made automatically when you insert data into it or switch using use <dbname>.
Before creating a database we should first learn about the naming restrictions for databases:
Database names must be case-sensitive.
The names cannot contain special characters such as /, ., $, *, |, etc.
MongoDB database names cannot contain null characters(in windows, Unix, and Linux systems).
MongoDB database names cannot be empty and must contain less than 64 characters.
For windows user, MongoDB database names cannot contain any of these following characters:
/\. "$*:|?
For Unix and Linux users, MongoDB database names cannot contain any of these following characters:
/\. "$
Example:
use LibraryDB # Switch to the LibraryDB database
Collection in MongoDB
A Collection in MongoDB is similar to a table in relational databases. It holds a group of documents and is a part of a database. Collections provide structure to data, but like the rest of MongoDB, they are schema-flexible.
Schema-flexible: MongoDB collections can store documents with different structures, and documents in the same collection donโt need to follow a uniform schema.
Multiple Collections: A database can have multiple collections, each holding different types of documents.
After creating database now we create a collection to store documents. The collection is created using the following syntax:
db.collection_name.insertOne({..})
Here, insertOne() function is used to store single data in the specified collection. And in the curly braces {} we store our data or in other words, it is a document.
Before moving further first you should learn about the naming restrictions for fields:
Fields in documents must be named with strings
The _id field is a reserved primary key that must be unique, immutable, and cannot be an array.
The field name cannot contain null characters.
The top-level field names should not start with a dollar sign($).
Document Size
Maximum BSON document size: 16MB, preventing excessive RAM or bandwidth usage.
For larger data, MongoDB provides GridFS.
Documents can contain duplicate fields.
Field order in MongoDB documents is preserved as inserted. The _id field is not required to be the first field. Renaming fields may affect their order.
_id Field in MongoDB
Every document must have a unique _id field, acting like a primary key.
Automatic ObjectId: If _id is not provided, MongoDB generates a unique ObjectId.
Custom _id: Users can assign a custom unique value for _id.
This will display the document stored in the books collection within the LibraryDB database.
Note: MongoDB automatically creates a unique index on the _id field for every collection. This index helps MongoDB quickly find documents based on their unique identifier.