0
0
MongoDBquery~5 mins

What is MongoDB

Choose your learning style9 modes available
Introduction

MongoDB helps you store and manage data in a way that is easy to use and flexible. It lets you save information like documents instead of tables.

When you want to save data that changes often and has different shapes.
When you need to build apps that work fast with lots of users.
When you want to store data like user profiles, messages, or product catalogs.
When you prefer a simple way to add or change data without strict rules.
When you want to handle big amounts of data that grow quickly.
Syntax
MongoDB
MongoDB stores data in collections of documents.
Each document is like a JSON object with fields and values.
You use commands like db.collection.find() to get data.
Documents are flexible and can have different fields.
Collections are like tables but without fixed columns.
Examples
Adds a new user named Alice with age 25 to the users collection.
MongoDB
db.users.insertOne({name: "Alice", age: 25})
Finds all users older than 20 years.
MongoDB
db.users.find({age: {$gt: 20}})
Updates the price of the product named Pen to 1.5.
MongoDB
db.products.updateOne({name: "Pen"}, {$set: {price: 1.5}})
Sample Program

This example switches to the 'shop' database, adds a product, and then retrieves all products.

MongoDB
use shop

// Add a product
 db.products.insertOne({name: "Notebook", price: 3.5, inStock: true})

// Find all products
 db.products.find()
OutputSuccess
Important Notes

MongoDB uses JSON-like documents, which makes it easy to understand and use.

It does not require a fixed schema, so you can add new fields anytime.

MongoDB is good for apps that need to grow and change quickly.

Summary

MongoDB stores data as flexible documents inside collections.

It is easy to add, find, and update data without strict rules.

It works well for apps with changing or large amounts of data.