Skip to main content

Mongodb-CheatSheet

 

All MongoDb commands you will ever need (MongoDb Cheatsheet)


In this post, we will see a comprehensive list of all the  MongoDB commands you will ever need as a MongoDB beginner. This list covers almost all the most used commands in MongoDB.

I will assume that you are working inside a collection named 'comments' on a MongoDB  database of your choice

1. Database Commands

View all databases

show dbs

Create a new or switch 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'})

Insert One Row

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}})

Comments

Popular posts from this blog

Connect and Configure MongoDB in Django Project

  Django, a high-level Python web framework, is well-known for its robustness and versatility. While Django natively supports popular databases like PostgreSQL and MySQL, you may want to integrate a NoSQL database like MongoDB for specific use cases. MongoDB’s flexibility and scalability make it an excellent choice for handling unstructured data. In this blog, we will walk you through the process of connecting MongoDB to a Django project, enabling you to harness the power of NoSQL in your web applications. Prerequisites: Before we begin, ensure you have the following prerequisites in place: Django installed on your system. MongoDB database server installed and running. Basic familiarity with Django project structure and database concepts. Virtual Environment, this is optional but recommended. You check our blog  here . Note:  For this tutorial, we are using our basic  skeleton project  for Django. You can also download the project from  here . Step 1: Insta...

Deploying/Host Django Project on PythonAnywhere

  Note  –   Before getting started  Sign up to PythonAnywhere  and Create Your Account .   We will be using Beginners’ plan which is a free plan. Disallowed Host Error If you get Disallowed Host at Django let just add  ‘*’  in  ALLOWED_HOSTS  in  settings.py  file and Reload the Web App. ALLOWED_HOSTS =['*'] Here’s an overview of the steps involved. Upload your code to Hosting Cloud Server Set up a virtualenv and install Django and any other requirements Set up your web app using the  manual config  option Add any other setup (static and media files, env variables)   Uploading Django Code to Hosting Server As our code is ready on  GitHub , we will clone it using  Bash Console.  Create a Bash Console by clicking on the Console Tab. You will see the terminal interface where you need to type git clone command. # for example git clone https://github.com/studygyaan/Django-CRM-Project.git Create ...

How to host Django Application in AWS using gunicorn & nginx in Production

in this post, we will see how to use    nginx  with    gunicorn  to serve    django     applications  in production.  Django is a very powerful web framework and ships with a server which is able to facilitate development. This development server is not scalable and is not suited for production. Hence we need to configure gunicorn to get better scalability and nginx can be used as a reverse proxy and as a web server to serve static files. Let's get started  Step 1 - Installing python and nginx Let's update the server's package index using the command below: sudo apt update Copy sudo apt install python3-pip python3-dev nginx Copy This will install python, pip and nginx server Step 2 - Creating a python virtual environment  sudo pip3 install virtualenv Copy This will install a virtual environment package in python. Let's create a project directory to host our Django application and create a virtual environment inside th...