This document provides a reference for common MongoDB commands and operations.
cls- Clear the screen.exit- Exit MongoDB shell.
mongosh- Start the MongoDB shell.
use TestDB- Switch to theTestDBdatabase. If it doesn't exist, it will be created.db.dropDatabase()- Drop the current database.
db.createCollection("students")- Create a collection namedstudents.
db.students.insertOne({name: "John", age: 25, gpa:3.2})- Insert one document into thestudentscollection.db.students.find()- Find all documents in thestudentscollection.db.students.insertMany([{name: "Jane", age: 22, gpa:3.5}, {name: "Doe", age: 23, gpa:3.8}])- Insert multiple documents into thestudentscollection.
- Insert a document with various data types:
db.students.insertOne({ "name": "larry", "age": 32, "gpa": 3.8, "fullTime": false, "registerDate": "2023-01-02T00:00:00Z", "graduationDate": null, "courses": ["ENG", "Math", "Chem"], "address": { "street": "123 KalirBazar", "city": "Narayanganj", "zipCode": 1300 } })
db.students.find({name: "Jane"})- Find documents where the name is "Jane".db.students.find().sort({name: 1})- Sort documents by name in ascending order.db.students.find().sort({name: -1})- Sort documents by name in descending order.db.students.find().sort({name: 1, age: -1})- Sort documents by name in ascending order and age in descending order.db.students.find().limit(2)- Limit the result to 2 documents.db.students.find().sort({gpa: -1}).limit(1)- Find the document with the highest GPA.db.students.find({}, {name: true})- Find documents and return only thenamefield.db.students.find({}, {_id: false, name: true})- Find documents and return only thenamefield without the_idfield.db.students.find({}, {_id: false, name: true, gpa: true})- Find documents and return thenameandgpafields without the_idfield.
db.students.updateOne({name: "Jane"}, {$set: {gpa: 3.9}})- Update the GPA of the first document where the name is "Jane".db.students.updateMany({name: "Jane"}, {$set: {gpa: 3.9}})- Update the GPA of all documents where the name is "Jane".db.students.updateOne({name: "Jane"}, {$unset: {gpa: ""}})- Remove the GPA field from the first document where the name is "Jane".
db.students.deleteOne({name: "Jane"})- Delete the first document where the name is "Jane".
db.students.find({name: {$ne: "Mridul"}})- Find all documents where the name is not "Mridul".db.students.find({age: {$gt: 25}})- Find all documents where the age is greater than 25.db.students.find({age: {$gte: 25}})- Find all documents where the age is greater than or equal to 25.db.students.find({age: {$lt: 25}})- Find all documents where the age is less than 25.db.students.find({age: {$lte: 25}})- Find all documents where the age is less than or equal to 25.db.students.find({name: {$in: ["Mridul", "Jane"]}})- Find all documents where the name is either "Mridul" or "Jane".db.students.find({$and: [{name: "Mridul"}, {age: 25}]})- Find all documents where the name is "Mridul" and the age is 25.db.students.find({$or: [{name: "Mridul"}, {age: 25}]})- Find all documents where the name is "Mridul" or the age is 25.db.students.find({$and: [{name: "Mridul"}, {age: {$gt: 25}}]})- Find all documents where the name is "Mridul" and the age is greater than 25.
🔔 MongoDB Setup : 👉 How to Setup Mongo DB
- Download MongoDB Community Version ( 👉 Click Here )
- Download MongoDB Shell ( 👉 Click Here )
- Cloud MongoDB Atlas ( 👉 Click Here )
- Setup Complete MongoDB in Local ( 👉 Click Here )
- Installing MongoDB & MongoDB Compass ( 👉 Click Here )
- CRUD Operations in MongoDB ( 👉 Click Here )
- Best way to Learn : ( 👉 Complete Playlist )
- Practice Source Code : ( 👉 GitHub Code )
🌱 More Learning Resources : ( 👉 Doc Link )
- Todo App using MongoDb and Host in Vercel
- Blog Website using MongoDb