Skip to main content

Command Palette

Search for a command to run...

MongoDB Fundamentals - (Basic Commands)

Published
0 min read
MongoDB Fundamentals - (Basic Commands)
V

Vincent is a Cloud DevOps Engineer with years of hands-on experience supporting, automating, and optimizing mission-critical deployments in Azure and AWS, leveraging configuration management, CI/CD, and DevOps processes. Strong background in Technical Operations and customer relations.

Let's talk about MongoDB - The database for modern applications

MongoDB is a general-purpose, document-based, distributed database built for modern application developers and the cloud era. No database makes you more productive.

Installation

Basic MongoDB commands

Open your terminal, command prompt, power shell or bash. Whichever you have installed on your system and let's get started

  • To start the MongoDB shell,

    mongo
    
  • Will create a database named amazon and switch to it

    use amazon
    
  • Will show you the current database you're in

    db
    
  • Will display all databases you have

    show dbs
    
  • Will create a collection called products with documents name and macbook value in the amazon database

    db.products.insert({name: "macbook"})
    
  • Will create a collection of users

    db.createCollection("users")
    
  • Will show a list of collections in your database

    show collections
    
  • Will delete the collection users from the database

    db.users.drop()
    
  • Will delete the entire database you are currently on

    db.dropDatabase()
    
  • Creates a collection products and inserts one document into it

    db.products.insertOne({name: "macbook", price: 1500, category: "Computers"})
    
  • Will help us find all the documents in the products collection

    db.products.find()
    
  • Will creates many documents into the product collection

    db.products.insertMany([{name: "iPhone 11", price: 1900, category: " Electronics"}, {name: "Headphone", price: 120, category: "Electronics" }])
    

Reading (flittering) query's from MongoDB

  • The find() helps us find every document in a collection e.g in products below

    db.products.find()
    
  • Returns the collection results in JSON format

    db.products.find().pretty()
    
  • Finds a specific item passed in, if it exists

    db.products.find({name: "macbook"})
    
  • Finds a specific key only

    db.products.find({}, {name: 1})
    
  • Finds a specific key only and hide the _id

    db.products.find({}, {name: 1, _id: 0})
    
  • Finds a specific key only, hide the _id and show only the first 2

    db.products.find({}, {name: 1, _id:0}).limit(2)
    
  • Finds every item that is less than 150

    db.products.find({price : {$lt: 150} })
    
  • Finds every item that is less than or equal to 150

    db.products.find({price : {$lte: 150} })
    
  • Finds every item that is grater than 800

    db.products.find({price : {$gt: 800} })
    
  • Finds every item that is grater than or equal to 800

    db.products.find({price : {$gte: 800} })
    
  • Finds every item that its price is less than 200 and belongs to the category of Electronics

    db.products.find({$and: [{price: {$lt:200}}, {category: "Electronics"} ]})
    
  • Finds every item that its price is greater than 500 or belongs to the category of Electronics

    db.products.find({$or: [{price: {$gt:500}}, {category: "Electronics"} ]})
    

Bonus

Click HERE to download PDF version of this tutorial

More from this blog

Vincent Iroleh

35 posts

This blog, curated by Vincent Iroleh, serves as a hub for fresh content and events focused on Azure, DevOps, AWS, Microsoft 365, and other Microsoft technologies. Thank you for your interest.