Mongodb: Data Modeling - Create Database, Drop Database
Mongodb: Data Modeling - Create Database, Drop Database
Create Database
'myDatabase' db.users.insertOne(
email: "[email protected]" }
);
.Drop Database
database
Output
10) CRUD (Create, Read, Update, Delete)
operations in MongoDB
db.users.insertOn
e({ name: "John
Doe", age: 28,
email: "[email protected]"
});
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();
db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 29 } }
);
db.users.updateMany(
{ age: { $lt: 30 } },
{ $set: { status: "young" } }
);
db.users.drop();
Output:
11) MongoDB : Document Querying and Functions
Query Operators
Array Queries
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
Drop a Collection
db.users.drop();
Output: