Assignment Of Day 3
MongoDB Basic Concepts
Assignment 1: Create a Database
Task:
Create a new database named Library and show the existing databases in the MongoDB instance.
Solution:
1. Open the MongoDB shell.
2. To create the Library database, use the use command:
use Library
(MongoDB will create the database once a document is inserted into it).
3. Now, to check if the database has been created, use the show databases command:
show databases
You should see the Library database listed after a document is inserted.
Assignment 2: Create a Collection
Task:
Create a collection named books in the Library database.
Solution:
1. After selecting the Library database, you can either insert a document into the books collection or explicitly create it:
use Library
db.createCollection("books")
2. To check if the collection was created, you can use the show collections command:
show collections
You should see the books collection listed.
Assignment 3: Insert a Document into Collection
Task:
Insert a document into the books collection in the Library database. The document should represent a book with the following fields: title, author, year, and genre.
Solution:
1. To insert a document into the books collection:
db.books.insertOne({
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"year": 1925,
"genre": "Novel"
})
2. After inserting, you can check if the document is added by querying the collection:
db.books.find()
Assignment 4: Insert Multiple Documents
Task:
Insert multiple documents into the books collection. Each document should represent a different book with the following fields: title, author, year, and genre.
Solution:
1. To insert multiple documents into the books collection:
db.books.insertMany([
{ "title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960, "genre": "Fiction" },
{ "title": "1984", "author": "George Orwell", "year": 1949, "genre": "Dystopian" },
{ "title": "Moby-Dick", "author": "Herman Melville", "year": 1851, "genre": "Adventure" }
])
2. After insertion, you can verify by running:
db.books.find()
Assignment 5: Query Documents
Task:
Query the books collection to find all books that were published after 1950.
Solution:
1. Use the find method with a query filter to retrieve books published after 1950:
db.books.find({ "year": { $gt: 1950 } })
2. This will return all books with a year field greater than 1950.
Assignment 6: Update a Document
Task:
Update the genre of the book "1984" to "Science Fiction" in the books collection.
Solution:
1. To update the document where title is "1984":
db.books.updateOne(
{ "title": "1984" },
{ $set: { "genre": "Science Fiction" } }
)
2. To verify the update, you can run:
db.books.find({ "title": "1984" })
Assignment 7: Delete a Document
Task:
Delete the book with the title "Moby-Dick" from the books collection.
Solution:
1. To delete the document where the title is "Moby-Dick":
db.books.deleteOne({ "title": "Moby-Dick" })
2. To verify if the document was deleted, you can run:
db.books.find({ "title": "Moby-Dick" })
Assignment 8: Count Documents
Task:
Count how many books are in the books collection.
Solution:
1. To count the total number of documents in the books collection:
db.books.countDocuments()
2. This will return the total number of books present in the collection.
Assignment 9: Find Documents with Specific Fields
Task:
Find all books that are written by "Harper Lee" and display only the title and author fields.
Solution:
1. Use the find method with a projection to return specific fields:
db.books.find(
{ "author": "Harper Lee" },
{ "title": 1, "author": 1, "_id": 0 }
)
2. This will return the titles and authors of all books written by Harper Lee, without the _id field.
Assignment 10: Index Creation for Efficiency
Task:
Create an index on the year field in the books collection to speed up queries that search by the year of publication.
Solution:
1. To create an index on the year field:
db.books.createIndex({ "year": 1 })
2. This will create an ascending index on the year field, improving the performance of queries that filter based on year.
3. You can check the created indexes with:
db.books.getIndexes()
Summary of Solutions:
- Databases: Represent containers for collections in MongoDB. Use the use command to switch between databases.
- Collections: Hold documents. Create collections either explicitly or implicitly when inserting data.
- Documents: Represent records in a collection, stored in BSON format. Each document can have fields of various data types.
- Queries: MongoDB allows querying using filters, projections, and operators like $gt (greater than).
- CRUD Operations: MongoDB supports insertOne, insertMany, updateOne, deleteOne, and more for managing documents.
These assignments cover essential operations in MongoDB, helping you understand the relationships between databases, collections, and documents.
