Introduction to MongoDB::
Document Database:
A record in MongoDB is a document, which is a data structure composed of field
and value pairs. MongoDB documents are similar to JSON objects. The values of
fields may include other documents, arrays, and arrays of documents.
A MongoDB document:
{
_id:xxxxxxxxxxx,
key:value,
key:value,
key:value
}
The advantages of using documents are:
1)Documents correspond to native data types in many programming languages.
2)Embedded documents and arrays reduce need for expensive joins.
3)Dynamic schema supports fluent polymorphism.
>mongod -----> to start the mongodb service from cmd
>ctrl+c -----> to stop the mongodb service.
>mongosh -----> to start the mongo shell from cmd
>ctrl+c ---> to stop the mongo shell.
Commonly used datatypes in mongodb :
1.String
2.Integer
3.Boolean
4.Double
5.Arrays
6.Object(for passing nested object)
7.Null
8.Date (to store the date time)
Here's a comprehensive guide on MongoDB, covering concepts, differences with RDBMS, installation, and basic operations:
1. What is MongoDB?
- MongoDB is a NoSQL, document-oriented database that stores data in JSON-like, flexible documents. This allows for dynamic schemas, meaning you can store and query data without needing to define the structure in advance.
2. Difference between MongoDB and RDBMS
| Feature | MongoDB | RDBMS (Relational Database Management System) |
|----------------------------|----------------------------------------------------|------------------------------------------------------------|
| Data Storage | JSON-like documents (BSON) | Tables with rows and columns |
| Schema | Dynamic, schema-less | Fixed schema with predefined structure |
| Joins | No joins; data is often embedded | Supports joins across multiple tables |
| Scalability | Horizontally scalable (sharding) | Vertically scalable (increasing resources on a single server)|
| Flexibility | High, allows for different document structures | Rigid, requires schema changes to accommodate new data types |
| Transactions | Supports multi-document ACID transactions | Supports ACID transactions |
| Data Relationships | Typically handled using embedded documents or references | Handled through foreign keys and joins |
| Use Cases | Ideal for hierarchical data, real-time analytics, big data | Suitable for complex queries, financial systems |
3. Installing MongoDB
# MongoDB Driver Installation
- For Node.js, install the MongoDB driver using npm:
npm install mongodb
# MongoDB Compass
- MongoDB Compass is a GUI tool that allows you to interact with your MongoDB databases visually.
- Download and Install: Visit [MongoDB Compass Download](https://www.mongodb.com/try/download/compass) and follow the installation instructions.
# MongoDB Shell (mongosh) Installation
- MongoDB Shell (mongosh) is a command-line tool that lets you interact with your MongoDB instance.
- Installation:
- For most OS, MongoDB Shell comes with MongoDB installation.
- You can also download it separately from the [MongoDB Shell Download](https://www.mongodb.com/try/download/shell) page.
4. MongoDB Basic Operations
# Create a Database
- In MongoDB, databases are created automatically when you insert data into them.
use myDatabase
# Create a Collection
- Collections in MongoDB are equivalent to tables in RDBMS.
- Like databases, collections are created when you insert the first document.
db.createCollection("myCollection")
# Insert Documents
- Insert a Single Document:
db.myCollection.insertOne({ name: "John", age: 25, city: "New York" })
- Insert Multiple Documents:
db.myCollection.insertMany([
{ name: "Jane", age: 28, city: "Los Angeles" },
{ name: "Mike", age: 32, city: "Chicago" }
])
# Find Documents
- Find All Documents:
db.myCollection.find()
- Find with a Condition:
javascript
db.myCollection.find({ age: 25 })
# Update Documents
- Update a Single Document:
db.myCollection.updateOne({ name: "John" }, { $set: { age: 26 } })
- Update Multiple Documents:
db.myCollection.updateMany({ city: "Chicago" }, { $set: { city: "Houston" } })
# Remove Documents
- Delete a Single Document:
db.myCollection.deleteOne({ name: "Mike" })
- Delete Multiple Documents:
db.myCollection.deleteMany({ age: { $gt: 30 } })
5. MongoDB Shell Commands
- Show Databases:
show dbs
- Switch to a Database:
use myDatabase
- Show Collections in a Database:
show collections
- Drop a Database:
db.dropDatabase()
- Drop a Collection:
db.myCollection.drop()
Summary
- MongoDB is a flexible, schema-less NoSQL database that differs from traditional RDBMS systems in its data storage and scalability approach.
- You can interact with MongoDB through the MongoDB Shell, programmatically using drivers like the Node.js MongoDB driver, or visually through MongoDB Compass.
- MongoDB operations revolve around CRUD (Create, Read, Update, Delete) principles, which are executed on collections of documents.
Comments
Post a Comment