In this repo we will work with:
- Postgresql to showcase relational DBMS
- MongoDB
The main purpose is to see the difference in terms of flexibility. Of course over Internet you will find much more explanation but don't hesitate to reach out if you have any question.
- Data source: https://www.nyc.gov/site/tlc/about/tlc-trip-record-data.page
- Work with MongoDB: https://github.com/sonyarianto/docker-compose-mongodb/blob/master/README.md
- https://www.baeldung.com/linux/mongodb-as-docker-container
You can specify a docker compose file in the docker-compose up command using the following command:
docker-compose -f docker-compose-file.yml upWhere docker-compose-file.yml is the name of your docker compose file.
Source: (1) docker compose up | Docker Documentation. https://docs.docker.com/engine/reference/commandline/compose_up/. (2) docker compose | Docker Documentation. https://docs.docker.com/engine/reference/commandline/compose/. (3) Use Docker Compose | Docker Documentation. https://docs.docker.com/get-started/08_using_compose/.
It is possible to use the mongo shell to interact with the MongoDB database. To do so, follow these steps:
-
Ensure that you have the MongoDB shell installed on your local machine. You can download it from the official MongoDB website: https://www.mongodb.com/try/download/shell.
-
Start the Docker Compose setup by running the following command in the terminal:
docker-compose up -d
# connect to pgcli (postgresql)
docker exec -it intro-db_postgres_1 psql -U username ny_taxiTo connect to a PostgreSQL database using pgcli, you can use the following command-line options:
-
Basic Connection:
- If you know the host address, port, username, and database name, you can use the following format:
pgcli -h <host> -p <port> -U <username> <dbname>- Replace
<host>,<port>,<username>, and<dbname>with your actual values.
- Replace
- If you know the host address, port, username, and database name, you can use the following format:
-
Using a URL:
- You can also connect directly using a URL:
pgcli 'postgresql://user:password@localhost:port/db_name'- Replace
user,password,localhost,port, anddb_namewith your specific details.
- Replace
- You can also connect directly using a URL:
-
Examples:
- Connect to a local database named
local_database:pgcli local_database - Connect to a remote database using a URL:
pgcli postgres://amjith:passw0rd@example.com:5432/app_db - Specify host and port explicitly:
pgcli -h localhost -p 5432 -U amjith app_db
- Connect to a local database named
Remember to adjust the parameters according to your PostgreSQL setup. Happy querying! 🐘
- Open a new terminal window and run the following command to enter the MongoDB container:
docker exec -it <container_id> mongo -u admin -p password --authenticationDatabase adminReplace <container_id> with the actual container ID of the MongoDB container. You can find the container ID by running docker ps command.
- You should now be in the MongoDB shell, connected to your database. You can execute various commands to interact with the database. For example, you can switch to a specific database, list collections, and perform queries.
Here's an example of using the mongo shell commands:
# Switch to the desired database
use sampledb
# Insert a document into the collection
db.samplecollection.insert({name: "Alice", age: 27})
# Query the collection
db.samplecollection.find()You can enter additional MongoDB shell commands as needed to interact with the database. The mongo shell provides a powerful interface for managing and querying MongoDB databases.
Remember to replace sampledb and samplecollection with your actual database name and collection name.
If you prefer a more interactive experience, you can also use MongoDB GUI clients like MongoDB Compass or Robo3T to connect to the MongoDB container and perform database operations.