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": "[email protected]",
"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()