Skip to content

Instantly share code, notes, and snippets.

@shankar-bavan
Last active September 17, 2019 09:01
Show Gist options
  • Save shankar-bavan/d07a9ca391bd1f3c77c0b732c25c2b22 to your computer and use it in GitHub Desktop.
Save shankar-bavan/d07a9ca391bd1f3c77c0b732c25c2b22 to your computer and use it in GitHub Desktop.
MongoDB Commands

Connect to MongoDB Shell

mongo

Exit the mongo Shell

quit()

Create Database

use DATABASE_NAME

Show Databases

show dbs

Drop Database

use DATABASE_NAME

db.dropDatabase()

Copy Database on Same Instance

db.copyDatabase("olddb","newdb")

Copy Database from Remote Instance

db.copyDatabase("remote_dbname","local_dbname","10.8.0.2","username","password")

Create Collection

db.createCollection("users")  

Show Collection

show collections

Insert Single Document

db.users.insert({
    "id": 1001,
    "user_name": "rahul",
    "name": [
        {"first_name": "Rahul"},
        {"middle_name": ""},
        {"last_name": "Kumar"}
    ],
    "email": "rahul@tecadmin.net",
    "designation": "Founder and CEO",
    "location": "India"
})

Search All Documents

db.users.find()

You can also use pretty() function with above command to show formatted output.

db.users.find().pretty();

Delete all documents from collection

db.users.remove({})

Drop collection

db.users.drop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment