MongoDB Command You Should Know 😇

MongoDB Command You Should Know 😇

Let’s dive in and learn some more cool MongoDB Command

MongoDB

Welcome back today we will learn some cool MongoDB commands.

start-the-game

🌟 What is MongoDB 🤔

MongoDB is an open-source document-oriented database that is designed to store a large scale of data and also allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in the MongoDB are not in the form of tables.

🌟 How it works 🤔

Now, we will see how actually thing happens behind the scene. As we know, MongoDB is a database server storing the data in these databases. Or in other words, the MongoDB environment gives you a server that you can start and then create multiple databases on it using MongoDB.
Because of its NoSQL database, the data is stored in collections and documents. Hence the database, collection, and documents are related to each other as shown below:

Database-collection-documents

🌟 Basic Commands

  1. How to check all the database

show dbs

2. How to switch to a different database
use myFirstDatabase

/* here myFirstDatabase is the name of my database */

3. How to check all the collections in the database

show collections

4. How to remove the database

db.dropDatabase()

/*Please note this command will remove the current database*

**To check the current database run the below command**

db

🌟 Create Document

/*Create a new document inside the specified collection */

👉 insertOne

db.users.insertOne({ name: “Atul” })

--------------------------------------------------------------------

/*Create multiple new documents inside a specific collection*/

👉 insertMany

db.users.insertMany([{ salary:10000 }, { salary: 20000 }])

🌟 Read Document

🌟 Delete Document

🌟 Update Document

  1. How to check for equality

$eq
db.users.find({ name: { $eq: “atul” }})

2. How to check for not equal

$ne
db.users.find({ name: { $ne: “atul” }})

3. How to check for greater than and greater than or equal to

$gtdb.users.find({ salary: { $gt: 20000}})

$gte
db.users.find({ salary: { $gte: 10000}})

4. How to check for less than and less than or equal to

$lt
db.users.find({salary:{ $lt: 20000}})

$lte
db.users.find({ age: { $lte: 10000}})

4. Check if a value is one of many values

$in
db.users.find({ name: { $in: [“atul”, “aman”]}})
/*It will fetch all the users whose name is atul and aman*/

5. Check if a value is none of many values

$nin
db.users.find({ name: { $nin: [“atul”, “aman”]}})

6. Check that multiple conditions are all true

$and
db.users.find({ $and: [{ salary: 200000}, { name: “atul” }]})

7. Check that one of the multiple conditions is true

$or
db.users.find({ $or: [{ salary: 20000}, { name: “atul”}]})

8. Negate the filter inside of $not

$not
db.users.find({ name: { $not: { $eq: “atul” }}})

9. Check if a field exists

$exists
db.users.find({ name: { $exists: true }})

🌟 Pagination and sort Document

  1. How to sort the data

sort
db.users.find().sort({ name: 1, age: -1})

/\ Here {name:1}---> sort in alphabetical order {age: -1} ---> it will sort in the reverse order */*

2. How to return only 10 records

limit
db.users.find().limit(10)

3. How to skip the first 5 records

skip
db.users.find().skip(5)

Thank-you


Thanks a lot for reading till the end 🙏 You can contact me in case if you need any assistance:
Email: atul19251@gmail.com
Web: https://iamatul.netlify.app/
Github: https://github.com/iamrajput
LinkedIn: https://www.linkedin.com/in/atul-kumar-singh-673357102/

Did you find this article valuable?

Support Dev Station by becoming a sponsor. Any amount is appreciated!