DEV Community

Nandhini D
Nandhini D

Posted on

Getting Started with MongoDB: Essential Queries

Introduction
I recently started learning MongoDB, a NoSQL database, and wanted to share some basic queries I practiced. MongoDB stores data in flexible JSON-like documents, which makes it really powerful for modern applications. MongoDB's flexible schema and powerful querying capabilities made it an excellent choice for this project.

Datadesign:
For this, I designed a collection named students with the following fields
{
"_id": "68a8360214d18e05a6850e6e",
"name": "John Doe",
"number_courses": 3,
"time_study": 4.508,
"marks": 19.202
}
DB name: Review

CRUD operation:

1.Create(C):

 db.Review.insertOne({
     "name": "Jane Smith",
     "number_courses": 4,
     "time_study": 5.0,
     "marks": 21.5
 });
Enter fullscreen mode Exit fullscreen mode

2. Top 5 students with highest marks:

    db.Review.find().sort({ marks: -1 }).limit(5);
Enter fullscreen mode Exit fullscreen mode

3.Query to count how many reviews contain the word “good”:

    db.Review.find({ Marks: { $gt: 40 } })
Enter fullscreen mode Exit fullscreen mode

4.Query to get all reviews for a specific ID:

   db.Review.find({_id:ObjectId('68a83923181c0b16f38c0ec6')})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)