Unit -3
Creating & Dropping Database & Collection:
Creating a Database:
use your_database_name
Example : - use studentDatabase;
Dropping a Database:
use your_database_name
db.dropDatabase()
Example:- studentDatabase.dropDatabase();
Creating a Collection:
db.createCollection("your_collection_name");
example:- studentDatabase.createCollection(“studentCollection”);
Dropping a Collection:
db.your_collection_name.drop()
Example:- studentDatabase.studentCollection.drop()
Batch Insert and Insert Validation:
Batch Insert:
db.your_collection_name.insertMany([
{ key: "value1" },
{ key: "value2" },
// ...
])
Insert Validation:
MongoDB does not enforce a schema by default, but you can validate inserts using JSON
schema:
db.createCollection("your_collection_name", {
validator: {
$jsonSchema: {
bsonType: "object",
properties: {
key: {
bsonType: "string",
description: "must be a string"
},
// ...
})
Query, Update & Delete Document using Modifiers:
Query Document:
db.your_collection_name.find({ key: "value" })
Update Document using Modifiers:
db.your_collection_name.update({ key: "value" }, { $set: { new_key: "new_value" } })
Delete Document:
db.your_collection_name.remove({ key: "value" })
Upserts, find(), limit(), skip(), sort():
Upserts (Update or Insert):
db.your_collection_name.update(
{ key: "value" },
{ $set: { new_key: "new_value" } },
{ upsert: true }
Find Documents:
db.your_collection_name.find({ key: "value" })
Limit Results:
db.your_collection_name.find().limit(5)
Skip Results:
db.your_collection_name.find().skip(10)
Sort Results:
db.your_collection_name.find().sort({ key: 1 }) // 1 for ascending, -1 for descending
Introduction To Indexing
Indexing, Compound Indexes:
Create an Index:
db.your_collection_name.createIndex({ key: 1 }) // 1 for ascending, -1 for descending
Compound Index:
db.your_collection_name.createIndex({ key1: 1, key2: -1 })
Using Index in a Query:
db.your_collection_name.find({ key: "value" }).hint({ key: 1 })
Explain and Hint:
db.your_collection_name.find({ key: "value" }).explain("executionStats")
Introduction to Indexing:
Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB
must scan every document in a collection to return query results. If an appropriate index
exists for a query, MongoDB uses the index to limit the number of documents it must scan.
Although indexes improve query performance, adding an index has a negative performance
impact on write operations. For collections with a high write-to-read ratio, indexes are
expensive because each insert must also update any indexes.
Index Basics:
Indexes improve query performance by allowing MongoDB to locate data more quickly.
They can be created on single fields or multiple fields (compound indexes).
Create an Index:
db.your_collection_name.createIndex({ key: 1 }) // 1 for ascending, -1 for descending
View Existing Indexes:
db.your_collection_name.getIndexes()
Types of Indexes:
1. Single Field Index: db.your_collection_name.createIndex({ key: 1 })
2. Compound Index: db.your_collection_name.createIndex({ key1: 1, key2: -1 })
3. Text Index: db.your_collection_name.createIndex({ text_field: "text" })
4. Geospatial Index: db.your_collection_name.createIndex({ location_field: "2dsphere" })
5. Hashed Index: db.your_collection_name.createIndex({ hashed_field: "hashed" })
Index Administration:
Drop an Index:
db.your_collection_name.dropIndex({ key: 1 })
Drop All Indexes:
db.your_collection_name.dropIndexes()
Rebuild All Indexes:
db.your_collection_name.reIndex()
View Index Usage Statistics:
db.your_collection_name.aggregate([{ $indexStats: {} }])