DEV Community

Cover image for MongoDB for Beginners
Madhuban Khatri
Madhuban Khatri

Posted on

MongoDB for Beginners

Hello Friends,
In this post, I will show how to use Basic Commands of MongoDB.

What is MongoDB ?

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License which is deemed non-free by several distributions.

What is MongoDB Compass

MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment. Compass is free to use and source available, and can be run on macOS, Windows, and Linux.

MongoDB Compass Image

We will use the following commands in MongoDB Compass.

Create database

use courses
Enter fullscreen mode Exit fullscreen mode

Show all databases

show dbs
Enter fullscreen mode Exit fullscreen mode

Delete the database

db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode

Show all the collections in current db

show collections
Enter fullscreen mode Exit fullscreen mode

Create a new collection

db.createCollection("<collection_name>")
Enter fullscreen mode Exit fullscreen mode

Delete a collection

db.courses.drop()
Enter fullscreen mode Exit fullscreen mode

Insert A Row in collection

db.courses.insertOne({"course": "MCA", "college": "MBM"})
Enter fullscreen mode Exit fullscreen mode

Insert many rows in collection (table)

db.courses.insertMany([{"course": "BSC", "college": "SBK"}, {"course": "BCA", "college": "JNVU"}])
Enter fullscreen mode Exit fullscreen mode

Get all the rows from collection

db.courses.find()
Enter fullscreen mode Exit fullscreen mode

Searching specific rows in collection

db.courses.find({"courses": "MCA"})
Enter fullscreen mode Exit fullscreen mode

Get the number of rows in collection

db.courses.find().size()
Enter fullscreen mode Exit fullscreen mode

Get limited records from collection

db.courses.find().limit(2)
Enter fullscreen mode Exit fullscreen mode

Ascending / Descending sorting

db.courses.find().sort({"course": 1})   // Ascending order
db.courses.find().sort({"course": -1})  // Descending order
Enter fullscreen mode Exit fullscreen mode

Get only one record

db.courses.findOne({"course": "MCA"})
Enter fullscreen mode Exit fullscreen mode

Update only one record in collection

db.courses.updateOne({"course": "MCA"}, {$set: {"course": "MBA"}})
Enter fullscreen mode Exit fullscreen mode

Update Multiple records in collection

db.courses.updateMany({"course": "MCA"}, {$set: {"course": "MBA", "college": "JNVU"}})
Enter fullscreen mode Exit fullscreen mode

Delete a record

db.courses.deleteOne({"course": "MCA"})
Enter fullscreen mode Exit fullscreen mode

Delete multiple records

db.courses.deleteMany({"course": "MCA", "course": "MBM"})
Enter fullscreen mode Exit fullscreen mode
Commands Explanation
use courses Create a Database
show dbs Show all database
db.dropDatabase() Delete Database
show collections Show the collections in DB
db.createCollection("") Create a collection
db.courses.drop() Delete a collection
db.courses.insertOne({"course": "MCA", "college": "MBM"}) Insert a row in collection
db.courses.insertMany([{"course": "BSC", "college": "SBK"}, {"course": "BCA", "college": "JNVU"}]) Insert many rows
db.courses.find() Fetch all rows
db.courses.find({"courses": "MCA"}) Fetch specific row
db.courses.find().size() Number of rows in the collection
db.courses.find().limit(2) Fetch Limited rows
db.courses.find().sort({"course": 1}) Ascending order
db.courses.find().sort({"course": -1}) Descending order
db.courses.findOne({"course": "MCA"}) Get only one record
db.courses.updateOne({"course": "MCA"}, {$set: {"course": "MBA"}}) Update only one record in collection
db.courses.updateMany({"course": "MCA"}, {$set: {"course": "MBA", "college": "JNVU"}}) Update multiple records in collection
db.courses.deleteOne({"course": "MCA"}) Delete a specific record
db.courses.deleteMany({"course": "MCA", "course": "MBM"}) Delete multiple records

Top comments (0)