Let’s dive in and learn some more cool MongoDB Command
MongoDB
Welcome back today we will learn some cool MongoDB commands.
🌟 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:
🌟 Basic Commands
- How to check all the database
show dbs
2. How to switch to a different databaseuse 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
- How to check for equality
$eqdb.users.find({ name: { $eq: “atul” }})
2. How to check for not equal
$nedb.users.find({ name: { $ne: “atul” }})
3. How to check for greater than and greater than or equal to
$gtdb.users.find({ salary: { $gt: 20000}})
$gtedb.users.find({ salary: { $gte: 10000}})
4. How to check for less than and less than or equal to
$ltdb.users.find({salary:{ $lt: 20000}})
$ltedb.users.find({ age: { $lte: 10000}})
4. Check if a value is one of many values
$indb.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
$nindb.users.find({ name: { $nin: [“atul”, “aman”]}})
6. Check that multiple conditions are all true
$anddb.users.find({ $and: [{ salary: 200000}, { name: “atul” }]})
7. Check that one of the multiple conditions is true
$ordb.users.find({ $or: [{ salary: 20000}, { name: “atul”}]})
8. Negate the filter inside of $not
$notdb.users.find({ name: { $not: { $eq: “atul” }}})
9. Check if a field exists
$existsdb.users.find({ name: { $exists: true }})
🌟 Pagination and sort Document
- How to sort the data
sortdb.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
limitdb.users.find().limit(10)
3. How to skip the first 5 records
skipdb.users.find().skip(5)
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/