MongoDB
MongoDB native
To connect to database
use the connect settings in mongodb atlas
Starting local database server
After installing, you can start the mongod by
sudo systemctl start mongodif you receive an error : Failed to start mongod.service: Unit mongod.service not found.
Run the following command first:
sudo systemctl daemon-reloadVerify that MongoDB has started successfully.
sudo systemctl status mongodYou can optionally ensure that MongoDB will start following a system reboot by issuing the following command:
sudo systemctl enable mongodStop MongoDB.
As needed, you can stop the mongod process by issuing the following command:
sudo systemctl stop mongodRestart MongoDB.
You can restart the mongod process by issuing the following command:
sudo systemctl restart mongodMongoDB setup Docker
sudo docker run --name mongo --network mongonet -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=pass \
mongo
sudo docker exec -it some-mongo shBegin using MongoDB.
Start a mongo shell on the same host machine as the mongod. You can run the mongo shell without any command-line options to connect to a mongod that is running on your localhost with default port 27017:
mongo "mongodb+srv://<username>:<password>@<url>:<port>/<db>"To show collections
show dbs
use <name of db>
show collectionsBasic Commands
List all databases :
show dbsto switch to db :
use <name_of_db>to run a query :
db.<name_of_collection>.[function name]to iterate over many results :
itadd :
.pretty()to see json betterto find any one document from collection, just use
.findOne()to create new collection :
db.createCollection("employees")
To shutdown db server
use admin
db.shutdownServer()
exitTo delete
drop collection
db.inspection.drop()Last updated
Was this helpful?