0% found this document useful (0 votes)
4 views

Mongodb Documentation

The document compares SQL and NoSQL databases, highlighting their definitions, data structures, examples, query languages, scalability, and use cases. It concludes that SQL is best for structured data and complex transactions, while NoSQL is suited for scalable and flexible big data applications. Additionally, it provides a guide on MongoDB commands for database management, including operations for databases, collections, and documents.

Uploaded by

rohit18raj10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Mongodb Documentation

The document compares SQL and NoSQL databases, highlighting their definitions, data structures, examples, query languages, scalability, and use cases. It concludes that SQL is best for structured data and complex transactions, while NoSQL is suited for scalable and flexible big data applications. Additionally, it provides a guide on MongoDB commands for database management, including operations for databases, collections, and documents.

Uploaded by

rohit18raj10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MD HASHIR IMTEYAZ

MONGODB

Difference Between Structured Query Language (SQL) and Unstructured Query Language (NoSQL)

Feature SQL (Structured Query Language) NoSQL (Unstructured Query Language)

A language used to interact with A set of technologies used to interact with


Defini on rela onal databases (RDBMS) where non-rela onal databases where data is stored
data is stored in structured tables. in a flexible, schema-less format.

Schema-less, allowing flexible data structures


Data Structured in tables with predefined
like documents, key-value pairs, columnar, or
Structure schemas.
graphs.

MySQL, PostgreSQL, Oracle, SQL


Examples MongoDB, Cassandra, CouchDB, Redis.
Server.

Query Uses SQL for querying (SELECT, Uses various query methods (e.g., JSON-like
Language INSERT, UPDATE, DELETE). queries in MongoDB, CQL in Cassandra).

Rigid schema; changes require Dynamic schema; data fields can vary across
Schema
altering tables. records.

Ver cally scalable (by upgrading


Scalability Horizontally scalable (by adding more nodes).
hardware).

Supports complex joins to retrieve Typically avoids joins; relies on embedding or


Joins
related data from mul ple tables. denormaliza on for rela onships.

Follows ACID (Atomicity, Consistency, O en follows BASE (Basically Available, So


ACID
Isola on, Durability) principles for state, Eventually consistent) for high
Compliance
data consistency. availability.

Best for structured data like financial Best for unstructured/semi-structured data like
Use Cases records, customer databases, and social media, IoT, big data, and real- me
transac onal systems. applica ons.

Conclusion

 SQL databases are ideal for applica ons requiring structured data, consistency, and complex
transac ons.

 NoSQL databases are suitable for highly scalable, flexible, and big data applica ons.

MongoDB Commands Documenta on

This documenta on provides a structured guide on using MongoDB commands for database
management, collec on opera ons, and document manipula on.
Default databases include:

 admin

 config

 local

1. Database Opera ons

1.1. Show All Databases

show dbs

1.2. Switch to a Database

use <database_name>

If the database does not exist, MongoDB creates it when a collec on is added.

1.3. Show Current Database

db.getName()

Returns the name of the currently selected database.

2. Collec on Opera ons

2.1. Show Collec ons in the Current Database

show collec ons

Displays all collec ons within the currently selected database.

2.2. Create a Collec on

db.createCollec on("<collec on_name>")

Creates a new collec on explicitly.

2.3. Drop a Collec on

db.<collec on_name>.drop()

Deletes a collec on from the database.

3. Document Opera ons

3.1. Insert a Document

db.<collec on_name>.insertOne({

name: "John Doe",


age: 30,

favourite_food: "pizza"

})

Inserts a single document into a collec on.

db.<collec on_name>.insertMany([

{ name: "Alice", age: 25 },

{ name: "Bob", age: 28 }

])

Inserts mul ple documents at once.

3.2. Find Documents

Find All Documents

db.<collec on_name>.find()

Returns all documents from the collec on.

Find Documents with a Condi on

 Find employees older than 30:

db.employees.find({ age: { $gt: 30 } })

 Find employees with age less than or equal to 50:

db.employees.find({ age: { $lte: 50 } })

 Find employees whose favorite food is pizza:

db.employees.find({ favourite_food: "pizza" })

Find a Specific Document

db.employees.findOne({ name: "John Doe" })

Returns the first matching document.

3.3. Upda ng Documents

Update One Document

 Update Willy's personal phone:

db.employees.updateOne(

{ name: "Willy" },

{ $set: { "phone.personal": "12345" } }

 Set privileges of Phob to "user":


db.employees.updateOne(

{ name: "Phob" },

{ $set: { privileges: "user" } }

Update Mul ple Documents

db.employees.updateMany(

{ age: { $lt: 30 } },

{ $set: { status: "young employee" } }

Updates mul ple documents matching the condi on.

4. Dele ng Documents

Delete One Document

db.employees.deleteOne({ name: "John" })

Deletes the first document matching the condi on.

Delete Mul ple Documents

db.employees.deleteMany({ status: "re red" })

Deletes all documents with status: "re red".

5. Sor ng and Filtering

Sort Documents by Name in Ascending Order

db.employees.find().sort({ name: 1 })

Sort Documents by Name in Descending Order

db.employees.find().sort({ name: -1 })

6. Bonus: Upda ng a Field for All Documents

Add bonus: 15 to All Employees with home < 10

db.employees.updateMany(

{ "home": { $lt: 10 } },

{ $inc: { "bonus": 15 } }

You might also like