knowledge-kitchen
/
course-notes
class: center, middle # MongoDB Setup Database Design --- # Agenda 1. [Overview](#overview) 1. [Server Setup](#server) 1. [Client Setup](#client) 1. [Conclusions](#conclusions) --- name: overview # Overview -- ## The Plan We will outline a few ways to gain access to a MongoDB **server** and use a MongDB **client** to issue commands to the database. Our discussion will include: -- - running a MongDB server locally using **MongoDB Community Server** -- , locally using a **Docker** image -- , and remotely using **MongDB Atlas** - a cloud-hosted solution. -- - running a MongoDB client locally by installing and using `mongosh` -- , and using a **driver** with any of today's popular programming languages --- name: server # Server Setup -- ## Choices of server There are several easy ways to try out a MongoDB **server**. -- - [MongoDB Community Server](https://www.mongodb.com/try/download/community) can be downloaded and tried locally on your own machine for free. -- - [Docker image of MongoDB](https://hub.docker.com/_/mongo) can be used to run a MongoDB server in a Docker container. -- - [MongoDB Atlas](https://www.mongodb.com/cloud/atlas) is a cloud-hosted database service with a free tier for new users and small-scale applications. --- template: server ## MongoDB Atlas Unless instructed otherwise, students are recommended to use [MongoDB Atlas](https://www.mongodb.com/cloud/atlas). -- - register using any credentials you like --- template: server ## MongoDB Atlas (continued) Unless your account already has a **cluster**, click to _create_ and _deploy_ one. ![Create a cluster](/content/courses/database-design/slides/assets/mongodb/mongodb-atlas-create-cluster.png) -- - select the _Free_ option -- - if asked for a "_Name_", "_Region_", or other cluster settings, use any you like. --- template: server ## MongoDB Atlas (continued again) There are a few access controls you will be prompted to enter: -- - **Database user**. You will be prompted to create a user which will be granted access to the cluster. Specify any _username_ and _password_ you like. Try not to lose these credentials. -- - **IP address whitelisting**. By default, the cluster will only allow connections from your current IP address. If you need any other IP address to be able to connect to the cluster, you can add it now or later. To whitelist all IP addresses (not a recommended practice for security reasons), you can add `0.0.0.0/0` to the whitelist. -- Once created, it may take a few minutes for the cluster to be deployed and ready to use. --- template: server ## MongoDB Atlas (continued once more) ![Access control](/content/courses/database-design/slides/assets/mongodb/mongodb-atlas-cluster-access-control.png) --- template: server ## MongoDB Atlas (continued yet again) Once the cluster is created, the interface will provide you with Connection information, currently available by clicking the `Connect` button. -- - Connecting via the **shell** is the default method to use unless instructed otherwise. -- - Note the **connection string**, which will be included in the given connection command, if using shell: `mongosh "mongodb+srv://your-cluster-subdomain.mongodb.net/" --apiVersion 1 --username your-username`. This is the string to connect via the `mongosh` client. -- - Depending on how you have chosen to connect (e.g. via shell or via a driver), this connection string or command may be different. -- - The connection details may ask you to install additional software, such as `mongosh` if using shell, or a driver if integrating the database into a program. Follow the instructions. --- template: server ## Docker container Arguably the easist way to run a MongoDB server locally is to create a Docker [container](/content/courses/software-engineering/slides/containers) from [the official MongoDB Docker image](https://hub.docker.com/_/mongo). This is a good option if you only need the database to be accessible by you on your own machine. -- To set up a server in this way, you must first... -- - create a [dockerhub](https://hub.docker.com/signup) account. -- - run the Docker engine in the background - this is most easily achieved on personal computers by downloading and running [Docker Desktop](https://www.docker.com/products/docker-desktop/). --- template: server ## Docker container (continued) To launch a local MongoDB server in a Docker container: -- - run the command, `docker run --name mongodb_dockerhub -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret -d mongo:latest` -- A new container named `mongodb_dockerhub` will be created, running the latest version of MongoDB on the default port `27017` of the local machine. The command also configures a default database user account with the username `admin` and password `secret`. --- name: client # Client setup -- ## Choices of client Regardless of which server you use, you must access it using a **client**. There are a few common MongoDB clients, including: -- - `mongosh` - the default command line client program. Download and install from [MongoDB's shell download page](https://www.mongodb.com/try/download/shell), if you haven't already. This can be run from the command line of your own local machine to connect to a server. -- - `mongoimport` and related tools to import and export data to/from the database using the command line. -- - [MongoDB Compass](https://www.mongodb.com/products/tools/compass) or similar Graphical User Interface (GUI) applications that provide a visual interface through which to view and manage data in the database. -- - "driver" modules, such as `pymongo` in Python or `mongoose` in Javascript, that allow code in any popular programming language to connect to a server. --- template: client ## mongosh Assuming you have installed it locally, use the MongoDB `mongosh` client to connect to the database server using your database's connection string: -- - The connection command given by **MongoDB Atlas** may look something like: `mongosh "mongodb+srv://your-cluster-subdomain.mongodb.net/" --apiVersion 1 --username your-database-username`. You will be prompted to enter your database user password after running that command. -- - If using a local server, such as **MongoDB Community Server**, connecting may be as simple as running the `mongosh` command with no arguments, or `mongosh --username your-username --password your-password` if you have required a username and password. -- - If running a local server in a **Docker container**, you can run `mongosh` within the container with a command such as `docker exec -ti mongodb_dockerhub mongosh -u admin -p secret`, where your container name and database user credentials may differ according to your setup. --- template: client ## mongosh (continued again) A database server can host many databases. Before performing any data-related operations, you must select the database you wish to use. -- - show the available databases: `show dbs` -- - select the specific database you would like to use: `use your_db_name`... this will create a new database if one does not yet exist by the given name. --- template: client ## mongosh (continued once more) Once connected, prove to yourself that you are logged into a [Javascript](/content/courses/agile-development-and-devops/slides/javascript-intro) command-line interface to MongoDB. -- - Type `1 + 1` and hit Enter. -- - Did it respond with `2`? -- If so, you're ready to start using your MongoDB database. --- template: client ## mongosh (continued for the last time) To exit `mongosh` at any time, simply type `exit` and hit Enter. --- template: client ## mongoimport The `mongoimport` command line utility is useful for importing bulk data from a text file into a MongoDB database collection. `mongoimport` supports `JSON` and `CSV` file formats. -- - install `mongoimport` and other [MongoDB Database Tools](https://www.mongodb.com/docs/database-tools/) from the [MongoDB Download Center](https://www.mongodb.com/try/download/database-tools) - the general pattern for using `mongoimport` is `mongoimport --host your_database_host --db your_database_name --collection your_collection_name --username your_username --password your_password --type json --file your_data_filename.json` --- name: conclusions # Conclusions -- Thank you. Good luck.