MongoDB Tutorial

This MongoDB tutorial teaches you how to store and query data with MongoDB, the most popular NoSQL document database. Instead of rows in tables, MongoDB stores data as flexible JSON-like documents, which map naturally to the objects you already use in JavaScript, Python and most other languages.

The tutorial uses MongoDB 7 and the mongosh shell, and later shows the same operations from Node.js with the official driver and with Mongoose.

What is MongoDB?

MongoDB is an open-source, document-oriented database. Each record is a document made of field and value pairs, similar to a JSON object. Documents are grouped into collections, and collections live inside a database.

Because documents in the same collection do not need identical fields, you can evolve your data model without running migrations for every small change.

Your First MongoDB Example

In mongosh, the shell for MongoDB, you insert a document and read it back like this. MongoDB creates the database and collection automatically on first insert.

use shop
db.products.insertOne({ name: "Green Tea", category: "Beverages", price: 6 })
db.products.find()
switched to db shop
{ acknowledged: true, insertedId: ObjectId('6720a1f4c2b9e81d4f3a1b01') }
[
  {
    _id: ObjectId('6720a1f4c2b9e81d4f3a1b01'),
    name: 'Green Tea',
    category: 'Beverages',
    price: 6
  }
]

Why Learn MongoDB?

  • It is the "M" in the MERN and MEAN JavaScript stacks
  • Documents match application objects, so there is less mapping code
  • Flexible schema makes prototyping fast
  • Built-in replication and sharding for high availability and scale
  • MongoDB Atlas offers a free cloud cluster for learning

MongoDB vs SQL Terms

  • Database → database
  • Table → collection
  • Row → document
  • Column → field
  • JOIN → embedded documents or $lookup
  • Primary key → the _id field

What This MongoDB Tutorial Covers

You will install MongoDB, learn document structure and BSON types, perform CRUD operations (create, read, update, delete), filter with query operators, sort and paginate, design schemas with embedding and references, add indexes, build aggregation pipelines, and finally connect from Node.js and Mongoose to build a small API-style example.

Frequently asked questions

Is MongoDB SQL or NoSQL?

MongoDB is a NoSQL database. It stores documents rather than rows and uses its own query language based on JSON-like objects, though it supports features once only found in SQL databases, such as transactions and joins via $lookup.

Is MongoDB free?

MongoDB Community Server is free to download and use. MongoDB Atlas, the cloud service, has a free M0 tier that is enough for learning and small projects.

When should I use MongoDB instead of MySQL?

MongoDB suits data that is naturally hierarchical or changes shape often, such as product catalogues, content and event logs. Choose a relational database when your data has many relationships and needs complex multi-table transactions.

Is MongoDB hard to learn?

No. If you know JSON and basic JavaScript, the query syntax will feel familiar quickly. Schema design is the part that takes more experience.