0
0
MongoDBquery~30 mins

MongoDB Shell (mongosh) basics - Mini Project: Build & Apply

Choose your learning style9 modes available
MongoDB Shell (mongosh) Basics
📖 Scenario: You are working as a data assistant for a small bookstore. You need to organize the book information in a MongoDB database so the store can easily find and update book details.
🎯 Goal: Build a simple MongoDB collection called books using the mongosh shell. You will insert book data, set up a query filter, find books by a condition, and finally update a book's information.
📋 What You'll Learn
Create a books collection with three book documents
Add a variable to filter books by a minimum number of pages
Use a find() query with the filter to get books with pages greater than or equal to the threshold
Update the price of a specific book using updateOne()
💡 Why This Matters
🌍 Real World
Bookstores and libraries use MongoDB to store and manage book information for easy searching and updating.
💼 Career
Knowing how to use the MongoDB shell to insert, query, and update data is essential for database administrators and backend developers working with NoSQL databases.
Progress0 / 4 steps
1
Create the books collection with initial data
In the mongosh shell, insert three book documents into the books collection with these exact fields and values: { title: 'The Hobbit', author: 'J.R.R. Tolkien', pages: 310, price: 15 }, { title: '1984', author: 'George Orwell', pages: 328, price: 12 }, and { title: 'To Kill a Mockingbird', author: 'Harper Lee', pages: 281, price: 10 }. Use the insertMany() method on db.books.
MongoDB
Need a hint?

Use db.books.insertMany() with an array of objects for the books.

2
Set a page count filter variable
Create a variable called minPages and set it to 300. This will be used to filter books with pages greater than or equal to this number.
MongoDB
Need a hint?

Use const minPages = 300 to create the variable.

3
Find books with pages >= minPages
Use db.books.find() with a filter object that selects books where the pages field is greater than or equal to minPages. Assign the result to a variable called booksWithMinPages.
MongoDB
Need a hint?

Use { pages: { $gte: minPages } } as the filter in find().

4
Update the price of '1984' to 14
Use db.books.updateOne() to find the book with title equal to '1984' and update its price field to 14. Use the $set operator.
MongoDB
Need a hint?

Use updateOne() with a filter and $set to change the price.