MongoDB - Overview

Search for a command to run...

No comments yet. Be the first to comment.
API (Application Programming Interface) security is critical for protecting sensitive data and maintaining the integrity of systems and applications. APIs are used to connect different systems and applications, and as a result, they can provide a gat...

Well after months of deep slumber, away from social media networks, I had to return to Pune and adapt to hybrid working. I was browsing through meetup.com and was intrigued by one event from AWS user group Pune. It is AWS Reinvent 2022 -Recap on 21st...

Episode 2

Simplify Access Control and Enhance Transparency Over Your ML Projects

Episode 1

In this blog, we are going to discuss about NoSQL Fundamentals, MongoDB basics and Advanced concepts
NoSQL can be defined as a database that is employed for managing the massive collection of unstructured data when your data is not piled up in a tabular format or relations like that of relational databases. The term NoSQL came from the word non-SQL or nonrelational. There are a wide variety of existing Relational Databases that have been unsuccessful in solving several complex modern problems such as:
A dynamic change in the nature of data - i.e., nowadays data are in structured, semi-structured, nonstructured as well as polymorphic in type.
The variety of applications and the type of data feed into them for analysis has now become more diverse and distributed and is approaching cloud-oriented.
Also, modern applications and services are serving tens of thousands of users in diverse geo-locations, having various time zones. So data integrity needs to be there at all the times.
Mongo DB can be defined as a document-oriented database system that uses the concept of NoSQL. It also provides high availability, and high performance, along with automatic scaling.
Database : In MongoDB, a database can be defined as a physical container for data collection.
Collection : Collections can be defined as a cluster of MongoDB documents that exist within a single database.
Document : A document can be defined as a collection of key-value pairs that contain dynamic schema.
Okay. The theory part is over. Let's get into hands-on part
To get started with MongoDB, you have to install it in your system. You need to find and download the latest version of MongoDB, which will be compatible with your computer system. You can use this http://www.mongodb.org/downloads
use cloudnloud_db;
db.dropDatabase();
show dbs;
db.createCollection("developers");
db.developers.drop();
insert()
db.collection_Name.insert(JSON document)
insertOne()
db.developers.insertOne({ _id: 1, name: "Veera" , skills : "blockchain"})
insertMany()
db.developers.insertMany(
[
{ _id: 2, name: "Kanna" , skills : "blockchain"},
{ _id: 3, name: "Karthik" , skills : "cybersecurity"},
{ _id: 4, name: "Deepika" , skills : "Node js"},
{ _id: 5, name: "Aadhi" , skills : "python"},
]
)
db.developers.find()
db.developers.find().pretty()
Filtering in Queries
db.developers.find( { skills: "blockchain"} )
db.developers.find({$or: [ { skills: "blockchain" }, { name: "karthik" } ] } )
db.developers.find( { "skills": { $in: [ "blockchain", "python" ] } } )
db.developers.update(
{_id: 4},
{
$set: { instrument: 'blockchain' }
}
)
db.collection.deleteOne()
db.developers.deleteOne( { name: { $in: [ "Veera", "Kanna"] } } )
db.collection.remove()
db.developers.remove( {} )
db.collection.deleteMany()
db.developers.deleteMany( { name: { $in: [ "Veera", "Kanna" ] } } )
MongoDB offers three different ways of performing aggregation:
db.developers.aggregate([{$group : {_id: "$skills", TotalRecords: {$sum : 1}}}])
db.techSubjects.find({}, {"topic":1, _id:0}).sort({"topic":1})
Hope you can understand the basic commands in MongoDB
let's move on Advanced concepts
Databases having indexes make queries performance more efficient.
When you have a collection with thousands of documents and no indexing is done, your query will keep on finding certain documents sequentially.
Create an Index in MongoDB
db.collection_name.createIndex( {KEY:1, KEY:2} )
db.Users.createIndex({"PhoneNo":1})
Indexing Fields for Sub-Documents
db.users.ensureIndex( {"location.city":1,"location.country":1} )
db.users.find({"location.city":"Los Angeles"})
mongod --port "PORT" --dbpath "your_db_complete_path" --replSet "instanceName_of_Replica_set"
mongod --port 27017 --dbpath " C:\Program Files\MongoDB\Server\4.0\data" --replSet rs0
Export to json
mongoexport --db cloudnloud_db --collection developers --out /developers.json
Export to CSV
mongoexport --db cloudnloud_db --collection developers -type = csv --out /developers.csv
mongodump
For creating a backup of your database in MongoDB, make use of the command mongodump.
mongodump -d <database name> -o <backup-folder>
mongodump -d cloudnloud_db -o /backup
mongostore
For restoring all your backup data in your server, MongoDB provides the command mongorestore.
mongorestore -d <database name> --dir <backup-folder>
mongorestore -d cloudnloud_db --dir /backup
Use $regex Operator
db.developers.find( {name : {$regex: "vee" }} )
Case Insensitive
db.developers.find( {name : {$regex : "vee", $options:"$i"} })
that's guys. Hope you can get better insights about MongoDB
Keep Learning & Keep Growing