📖
System Design
  • Home
  • Learn System Design Daily
  • System Design Steps
  • List of design questions:
  • My System Design Interview Checklist
  • CheatSheet
  • Programming Language Jargons
  • Scaleable Design
  • Agile Scrum
  • Uber
  • Gmail System Design
  • Distributed Rate Limiting
  • Audio Search Engine
  • Code Style and Review
  • Calling App Design
  • Low Level Design: Payment Tracking App
  • Machine Coding :Cache
  • Interview Advice
  • URL Shortner
  • Unique ID in Distributed Systems
  • Load Balancing Algorithm
  • API Architecture
  • Desgin Practise for Rest API
  • Performance Practise for API
  • API Gateway
  • API Security Hacks
  • Distributed Design Patterns
  • Fault Tolerance in Distributed Systems
  • Microservice Communication Design Patterns
  • Zipping /Compression
  • Database
  • Mongo DB
  • SQL
  • PostgreSQL
  • Database Designs
    • Designing a location based database
  • Building Database
  • Design Patterns
    • Microservice Architecture 10 Design Patterns
    • Interaction Patterns
  • Locale.ai
  • Version Control
  • Caches / Caching
  • High Level Design
  • Low Level Design
  • Containers, Docker
  • Docker
  • Linux Directories
  • Design Pattern for Software Architect
  • S.O.L.I.D Principles
  • Monitoring and Telemetry for Production System
  • C4 model
  • LRU Cache
  • VSCode
  • Chatbot Architecture
  • Streaming API Repsonse
  • Latency in System Design
  • Cloud
    • Azure
    • AWS
  • Builds
    • Jenkins
Powered by GitBook
On this page

Was this helpful?

Mongo DB

Launching the Mongo DB shell
>> mongo

List all database  
>> show dbs

Select a database from list (admin is in list)
>> use admin 

show collections
>> show collections

Use js in shell to run query such as 
>> db.system.version.find()
// will list all value

>> db.system.version.count()
//will list count of collections

Create a database
>> use myFirstDatabase

Create new collection
>> db.createCollection("myFirstCollection")

Now Show Dbs
>> show dbs

Show Collections
>> show collections

----------------------------------------------

Drop a database
>> db.dropdatabase()

create database
>> use blog

Insert into collection
>> db.posts.insert({
title:"mongodb rules",
body:" my first mongodb document",

// time in 24hrs
date:: ISODate("2020-0901T18:30:00Z"),

tags:["mongodb","databases"],

likes:2,
author: {
        name:"Harshit",
        surname:"Yadav"},
location:{
        type:"Point",
        coordinates:[-112.45,37.76]
        
        }
})

List all Documents
>>db.posts.find()

List all documets in pretty format
>>db.posts.find().pretty()

----------------------------------------

Schema can also be enforced in a database

Find blog Post in a database
>> db.posts.find({title:"Second Post"}).pretty()

Sort titles by Ascending Order
>> db.posts.find().sort({title:1}).pretty()

Sort titles by descending Order
>> db.posts.find().sort({title:-1}).pretty()

Find Posts with greater then 2 Likes and sort
>> db.posts.find({likes:{$gt:2}}).sort({likes: -1}).pretty()

Find Posts with greater equal to 2 Likes
>> db.posts.find({likes:{$gte:2}}).pretty()

Find Posts with  equal to 2 Likes 
>> db.posts.find({likes:{$eq:2}}).pretty()


Update a field in document
>> db.post.update ({
title:" Second Post",      
},
{
 $set: {
         tags:['tag1'],
         location: {
         type :"Point",
         coordinates :[-73.97,40.77]
 
         }
 }
 })
 
 Delete a field in document
 >> db.posts.update(
 {
         title:"Second Post"
         
 },
 {
     $unset: {
     tags: ""
     }    
 }
 )
 
 ---------------------------------------------
 
 Insert many documents in database
 >> db.posts.insertMany(
 [
         {
                 title:"Awesone post",
                 body:"Body of awesome post",
                 date: ISODate("2019-11-01T03:30:00Z)
                 
         },
          {
                 title:"Awesone post",
                 body:"Body of awesome post",
                 date: ISODate("2019-11-01T03:30:00Z)
                 likes:3,
                 
         },
                   {
                 title:"Awesone post",
                 body:"Body of awesome post",
                 date: ISODate("2019-11-01T03:30:00Z)
                 location:{
                        type:"Point",
                        coordinates:[-112.45,37.76]
        
        }
                                 
         },

 ]
 )
 
 
 Update one  documents with a field
 >> db.posts.update()
 (        
         {},
         {        
                 $set:{
                         published:false:
                 }
         }
 )
 
 
Update All  documents with a field
 >> db.posts.update()
 (        
         {},
         {        
                 $set:{
                         published:false:
                 }
         },
         {
                 multi:true
         }
 )
 
 AND operator queries
 >> db.posts.find(
         {
            $and:[
            {
                    title:"Awesome Post"
            },
            {
                 likes:{
                         $exists:true
                       }   
            }
            
            ]     
         }
         
 )
 
 
 -----------------------------------------
PreviousDatabaseNextSQL

Last updated 4 years ago

Was this helpful?