MongoDB
1. Installing mongoDB
- Run a mongoDB server using docker
Go to the Official Image of mongo:
https://hub.docker.com/_/mongo
In the "How to use this image" section, you'll see the following command:
docker run --name mongo -d -p 27017:27017 mongo:6
Or with a password:
docker run --name mongo -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret mongo
And, if you wanna add extra security, use a certificate:
docker run --name mongo -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
-v ~/Desktop/certificates:/certs:ro \
mongo \
--tlsMode requireTLS \
--tlsCertificateKeyFile /certs/server.pem \
--tlsCAFile /certs/rootCA.crt
See how to create a certificate over at the certificates guide.
You now should have a container running a MongoDB server listening on the standard MongoDB port 27017.
In the next section we'll be testing the connection to our server, stay tunned.
2. Test Server Connectivity
Before using the CLI tool, check to see that the mongo server's logs are ok:
docker logs mongo
Or you can ssh into it using:
docker exec -it mongo bash
Second, make sure you have mongo CLI tool installed.
mongocli
Or...
mongosh
If not, install it:
- MacOS:
Use homebrew
to install (the official recommended way):
brew install mongocli
You now have access to the global command mongosh
.
- Windows:
Go to: https://www.mongodb.com/docs/mongocli/current/
And hit Install MongoDB CLI, then hit the Download button.
Once installed, run the following command in your terminal:
mongosh "mongodb://localhost:27017"
Or, if you have a password:
mongosh --host localhost:27017 --username mongoadmin --password secret --authenticationDatabase admin
Or, if you have a certificate:
mongosh --tls --tlsCAFile rootCA.crt --tlsCertificateKeyFile client.pem --host localhost:27017 --username mongoadmin --password secret --authenticationDatabase admin
All of the commands below work as well:
mongosh localhost:27017
mongosh mongodb://localhost:27017
mongosh --host localhost --port 27017
mongosh --host localhost:27017
mongosh --host 127.0.0.1 --port 27017
You should see the following output:
Current Mongosh Log ID: 67909697207e4a089faba1d2
Connecting to: mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.3.3
(node:56199) ExperimentalWarning: CommonJS module /opt/homebrew/Cellar/mongosh/2.3.3/libexec/lib/node_modules/@mongosh/cli-repl/node_modules/@mongodb-js/devtools-proxy-support/dist/fetch.js is loading ES Module /opt/homebrew/Cellar/mongosh/2.3.3/libexec/lib/node_modules/@mongosh/cli-repl/node_modules/node-fetch/src/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Using MongoDB: 6.0.20
Using Mongosh: 2.3.3
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
------
The server generated these startup warnings when booting
2025-01-22T06:56:17.488+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2025-01-22T06:56:18.084+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2025-01-22T06:56:18.084+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
2025-01-22T06:56:18.084+00:00: vm.max_map_count is too low
------
test>
You should now be able to run mongodb commands.
3. Run Basic MongoDB commands
- command 1: adminCommand
The command's form
db.adminCommand({ getCmdLineOpts: 1 })
Description
Check to see all running configuration.
- command 2: show all existing databases
The command's form
show dbs
Description
Shows a list of all the databases that are in the cluster.
Note! Do not touch the "admin" and the "local" databases.
The admin
database stores all the user/s with admin privileges, that are able to connect to the cluster.
- command 3: use
The command's form
use dbName
Description
Selects a specific database.
Note: If you use some dbName
that doesn't exist, You'll still get a message saying "switched to
db dbName". But if you do "show dbs", you won't see it on the list. What this means is that mongoDB wanted to be so flexible as to say "Yeah, we're ready to create that db for you if you desire, but technically? right now? It doesn't exist".
- command 4: show collections
The command's form
show collections