0% found this document useful (0 votes)
1K views8 pages

Mongodb: Data Modeling - Create Database, Drop Database

The document provides instructions on how to create and drop databases in MongoDB, including commands for inserting, reading, updating, and deleting documents. It covers CRUD operations with examples for inserting single and multiple documents, querying data with specific conditions, and modifying or deleting documents based on criteria. Additionally, it explains sorting, limiting, and skipping results in queries.

Uploaded by

cammusowmiya13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views8 pages

Mongodb: Data Modeling - Create Database, Drop Database

The document provides instructions on how to create and drop databases in MongoDB, including commands for inserting, reading, updating, and deleting documents. It covers CRUD operations with examples for inserting single and multiple documents, querying data with specific conditions, and modifying or deleting documents based on criteria. Additionally, it explains sorting, limiting, and skipping results in queries.

Uploaded by

cammusowmiya13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

9) MongoDB : Data Modeling – Create

Database, Drop Database

Create Database

use myDatabase; // Switch to or create a new database named

'myDatabase' db.users.insertOne(

{ name: "Jane Doe", age: 30,

email: "[email protected]" }
);

.Drop Database

use myDatabase; // Switch to the database you want

to drop db.dropDatabase(); // Drops the current

database
Output
10) CRUD (Create, Read, Update, Delete)
operations in MongoDB

Create (Insert Data)

Insert a single document

db.users.insertOn
e({ name: "John
Doe", age: 28,
email: "[email protected]"
});

Insert multiple documents

db.users.insertMany([
{
name: "Alice Smith",
age: 34,
email: "[email protected]"
},
{
name: "Bob Johnson",
age: 25,
email: "[email protected]"

}
]);
Read (Query Data)
Find all documents in a collection

db.users.find();

Find a specific document by field db.users.find({ name: "John Doe" });


Update (Modify Data)

Update a single document

db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 29 } }
);

Update multiple documents

db.users.updateMany(
{ age: { $lt: 30 } },
{ $set: { status: "young" } }
);

Delete (Remove Data)

Delete a single document

db.users.deleteOne({ name: "Alice Smith" });

Delete multiple documents

db.users.deleteMany({ age: { $lt: 30 } });

Drop a collection (delete all documents in it)

db.users.drop();
Output:
11) MongoDB : Document Querying and Functions

Basic Document Querying

Find All Documents

db.users.find(); // Returns all documents from the 'users'


collection

Find Documents with a Specific Field Condition


db.users.find({ age: 25 }); // Find all users with age 25

Find One Document


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

Query Operators

Query with $gt (Greater Than)


db.users.find({ age: { $gt: 30 } });
// Returns users older than 30

Array Queries

db.users.find({ hobbies: "reading" });


// Returns users whose 'hobbies' array contains "reading"

Sort, Limit, and Skip

Sort Documents
db.users.find().sort({ age: 1 }); // Sort by 'age' in ascending
order
db.users.find().sort({ age: -1 }); // Sort by 'age' in descending
order

Limit Results
db.users.find().limit(5); // Returns only the first 5 documents

Skip Results
db.users.find().skip(10).limit(5);
// Skips the first 10 documents and returns the next 5 documents
Delete Documents

Delete One Document


db.users.deleteOne({ name: "John Doe" });
// Deletes the first document where name is "John Doe"

Delete Multiple Documents

db.users.deleteMany({ age: { $lt: 20 } });


// Deletes all users with age less than 20

Drop a Collection
db.users.drop();
Output:

You might also like