Today's Must-Know Tips for Using MongoDB

Today's Must-Know Tips for Using MongoDB

What is MongoDB?

MongoDB is a document-oriented NoSQL database system that provides high scalability, flexibility, and performance. Unlike standard relational databases, MongoDB stores data in a JSON document structure form. This makes it easy to operate with dynamic and unstructured data and MongoDB is an open-source and cross-platform database System.

Database

  • Database is a container for collections.

  • Each database gets its own set of files.

  • A single MongoDB server can have multiple databases.

Collection

  • Collection is a group of documents.

  • Collection is equivalent to RDBMS table.

  • A collection consists inside a single database.

  • Collections do not enforce a schema.

  • A Collection can have different fields within a Documents.

Why Use MongoDB?

Document Oriented Storage − Data is stored in the form of JSON documents.

  • Index on any attribute: Indexing in MongoDB allows for faster data retrieval by creating a searchable structure on selected attributes, optimizing query performance.

  • Replication and high availability: MongoDB’s replica sets ensure data redundancy by maintaining multiple copies of the data, providing fault tolerance and continuous availability even in case of server failures.

  • Auto-Sharding: Auto-sharding in MongoDB automatically distributes data across multiple servers, enabling horizontal scaling and efficient handling of large datasets.

  • Big Data and Real-time Application: When dealing with massive datasets or applications requiring real-time data updates, MongoDB’s flexibility and scalability prove advantageous.

  • Rich queries: MongoDB supports complex queries with a variety of operators, allowing you to retrieve, filter, and manipulate data in a flexible and powerful manner.

  • Fast in-place updates: MongoDB efficiently updates documents directly in their place, minimizing data movement and reducing write overhead.

  • Professional support by MongoDB: MongoDB offers expert technical support and resources to help users with any issues or challenges they may encounter during their database operations.

  • Internet of Things (IoT) Applications: Storing and analyzing sensor data with its diverse formats often aligns well with MongoDB’s document structure.

Where to Use MongoDB?

  • Mobile and Social Infrastructure

  • Data Hub

  • Previous Pag

  • Big Data

  • User Data Management

  • Content Management and Delivery

1. Database Commands

View all databases

use dbName

View current Database

db

Delete Database

db.dropDatabase()

2. Collection Commands

Show Collections

show collections

Create a collection named 'comments'

db.createCollection('comments')

Drop a collection named 'comments'

db.comments.drop()

3. Row(Document) Commands

Show all Rows in a Collection

db.comments.find()

Show all Rows in a Collection (Prettified)

db.comments.find().pretty()

Find the first row matching the object

db.comments.findOne({name: 'Harry'})
db.comments.insert({
    'name': 'Harry',
    'lang': 'JavaScript',
    'member_since': 5
 })

Insert many Rows

db.comments.insertMany([{
    'name': 'Harry',
    'lang': 'JavaScript',
    'member_since': 5
    }, 
    {'name': 'Rohan',
    'lang': 'Python',
    'member_since': 3
    },
    {'name': 'Lovish',
    'lang': 'Java',
    'member_since': 4
}])

Search in a MongoDb Database

db.comments.find({lang:'Python'})

Limit the number of rows in output

db.comments.find().limit(2)

Count the number of rows in the output

db.comments.find().count()

Update a row

db.comments.updateOne({name: 'Shubham'},
{$set: {'name': 'Harry',
    'lang': 'JavaScript',
    'member_since': 51
}}, {upsert: true})

Mongodb Increment Operator

db.comments.update({name: 'Rohan'},
{$inc:{
    member_since: 2
}})

Mongodb Rename Operator

db.comments.update({name: 'Rohan'},
{$rename:{
    member_since: 'member'
}})

Delete Row

db.comments.remove({name: 'Harry'})

Less than/Greater than/ Less than or Eq/Greater than or Eq

db.comments.find({member_since: {$lt: 90}})
db.comments.find({member_since: {$lte: 90}})
db.comments.find({member_since: {$gt: 90}})
db.comments.find({member_since: {$gte: 90}})

Stay Tuned for More:

Stay connected on LinkedIn: LinkedIn Profile

Stay up-to-date with GitHub: GitHub Profile

Feel free to reach out to me, if you have any other queries.

Happy learning, MongoDB explorers! 🚢🌐