MongoDB Commands
Install MongoDB NoSQL Database:
MongoDB is very easy to install, and it is present in Ubuntu 20.04 software repository.
- sudo mongod
- sudo mongo
- show dbs
- use dbName
- db
- db.dropDatabase
- db.reateCollection(“collectionName”)
- db.collectionName.drop
- db.collectionName.insert()
- show collections
- db.collectionName.find()
Starting MongoDB Server
You can start MongoDB on your computer with the mongod
command.
sudo mongod
Keep the mongod
window running when you want to work with your local MongoDB. MongoDB stops when you close the window.
Start MongoDB Client
One way to add items to a MongoDB database is through the Mongo Shell. To open up the Mongo Shell, you open another command line window and run mongo
.
sudo mongo
Note: Make sure you keep the mongod
window open! You won’t be able to interact with the Mongo Shell if you close the mongod
window.
Show All Databases
Useshow dbs
command from mongo shell to list all the available databases on MongoDB server. This will show the database name with there size.
show dbs
I currently have three databases. They are: admin
, config
and local
.
Go to a Particular Database
Create a database called databaseName
. You can use the use <database>
command to create and switch to a new database.
use databaseName
Create Collection
Create a collection called collectionName
. You can use the db.createCollection("<collection name>")
to create a new collection.
db.createCollection(“collectionName”)