Updated November 13, 2022
For those whom must use the lab computers, there is an alternative for now to use the commandShell for MongoDB to access your database. Instructions for this option are coming soon. For now you look in MyApps for MongoDB you will be able to get it running with both Compass and the Command Shell.
We are currently requesting that the version of VSCode on MyApps be updated to a version compatible with the extension we are going to use, but this will take time and likely will not be ready until the winter term.
In this course we will study MongoDB and therefore we will need to prepare our laptops/personal computers for working with MongoDB. There will be two steps in the process:
If you already have an Atlas account setup from Web322, then you will simply need to login to your atlas account to obtain your connection string.
If you already have VS Code installed, at least version 1.6 or above, you can skip that step and just install the extension.
For those students whom are taking, or have taken Web322 you will likely already have an Atlas account, if this is the case, you can skip this entire step and move to the VS Code setup step.
At this point create a cluster name, that is personal to you. It is best if the name has no spaces or other special symbols
You will then have to create the first username and password for accessing the database. This should be different than your Atlas password, as this one you may have to share with your group.
Once the user is created, it should show up just below the creation form. You may choose to add more now, but for classes, it is not necessary.
Once you are done this step, you will be returned to the same screen in which you will be at when you login from now on.
You will need to ensure that you are allowing access to the database from different computers. In the real world, this would normally be only accessed from the web or cloud servers. But for the purposes of class, and ease of setup, we will "Allow Access From Anywhere" to start. This is accomplished by clicking on the Connect button beside your cluster name, in the previous screen and then choosing the appropriate option under "Add a connection IP Address"
After access is granted, a screen comes up that that will look like one of 2 different screens: pending how your account is setup. If you have the choice to choose "Connect using VS Code" do so, if not choose "MongoDB Compass". Both will give you the same connection string, which is really all we want at this point.
Both option will bring up a screen that shows a connection string that looks something like below.
There are three parts to this connection string you will need to know about.
If you do not already have VS Code installed, it is fairly straight forward to install.
To interact with MongoDB on a scripting basis, we will need a user interface. The newly created VSCode Extension plugin is really well done, and was created by the MongoDB community.
To add the extension, open VS Code and:
Once you have an Atlas account and VS Code installed, now you can now add a connection and user your first playground.
Clicking on "add a Connection" bring up the following screen, on which you will want to choose to connect with a connection string.
Alternatively, you can go to the view menu, then Command Pallete and type in MongoDB where one option will be to connect with a connection string.
Near the top will be a textbox to enter your connection string. This is obtained from your Atlas cluster account, by clicking on the "Connect" option. See above! Paste your connection string in here.
Change the password to be your password. Be sure to remove the angle braces!
Replace the name of the database (currently test) with a name of a new or existing database. If it does not exist, it will be created when you enter your first data, otherwise it will connect to the existing one.
Press Enter to attempt the connection. You should see a pop-up in the bottom right stating you connected successfully.
On the left side, under connections (if the leaf icon is selected) will be your new connection. The first thing you should do is to right click on that connection and choose to rename it. The default name is the server name, and different databases on the same server will all have the same name, making it hard to know which is which.
Once you are successfully connected, you will create a playground. Go to View/Command Pallets and when typing in Mongo you will see "Create MongoDB Playground".
This will have brought up a new file and a default scripting template.You can review this the first time to see how it works, but then you can clear the contents. In the extension settings, you can choose not to have the template come up each time you start a new playground.
There is one setting you will want to change right away. There is a confirmation pop-up that comes up every time you run a command, it gets old very quickly. Go to File/Preferences/Settings and then expand the MongoDB settings. The first option is "confirm run all". I suggest making sure it is unchecked.
Now you are connected and have an active playground, you can type in a command in the window, and then click the run button (arrow in top right corner) and you will see a new docked window appears with the output.
Try a few more commands:
The following is currently not needed for DBS311 or DBS710 students. But the legacy instructions are left here in case for your own use.
Go to the following link to download MongoDB:
https://www.mongodb.com/download-center/community
Download
and remember where you saved the file.Read and accept the License Agreement, click next
Choose a Complete
Setup Type
On the next configuration screen, leave the settings as default unless you have a separate data drive on your computer where you might want to store the data files.
It is recommended to move the data and log files to a data drive if available. Otherwise, leave the settings as the default ones.
Click Next
On the next screen, leave "Install MongoDB Compass" checked and click Next
. Compass is a GUI for MongoDB that let's you run AdHoc Queries in seconds and interact with your data with full CRUD capabilities.
On the last setup screen, click Install
. Note, you will need to be an administrator on the machine to do this, if you are prompted for administrative credentials, enter them, otherwise, just click on Yes to give Mongo Permissions to make changes to your computer.
When the installation finishes, click the Finish
button.
If Compass opens as part of the installation process, that is okay, just minimize it for now, we will come back to that later.
Some people may get an error that the service did not start properly, this is actually okay at this point, you can click ignore, if you get this pop-up error.
c:\Program Files\MongoDB\Server\5.0\binType
Mongo
at the command prompt to run the client shell.> use libraryThe “use” command, makes a database your current one. However, since we have not inserted data into the “library” database, you will not see this database as an existing one. We run the “use library” command to tell MongoDB that our current database will be the “library” database.
> db.book.insertOne({"title": "Blue Sky"})
> show dbs
If everything goes according to plan, then we are all good.
Throughout this course, almost everything we need to do with MongoDB can be done at the command prompt or using Compass as a GUI. This is really personal preference, and this website will present both options for you to use. Ultimately, it is the commands that you need to know how to use to create and manipulate your database structure and the data.
When you first open Compass for the first time you should see the dashboard screen that looks like this:
If installation went as expected above, then you can type the following in for the connection string:
mongodb://localhost:27017
If you choose to use Atlas for your database hosting location, that is fine, but this website documentation will refer to localhost throughout, you will need to adjust this information to your Atlas connection string where appropriate.
After the connection string is entered, clicking connect should show the following screen:
Note: if you followed all the instructions in the command prompt section, you should see the library database in the list.
like in the previous instructions, let us create a new database and add a book.
Click Create Database
and the following screen should appear.
Enter "library2" for the Database Name and "Magazine" for the Collection and then click Create Database
. This creates a new database with a collection of objects called "Magazine" inside it.
Within Compass you can see these collections, by clicking on the database name. Click on library to see the book collection. Note that from this screen you can add additional collections or delete existing ones.
Then click on book to see the "Blue Sky" book we entered earlier. You can also add new data from this screen by importing a file or inserting a document.
Importing a file allows you to import JSON or CSV files into the collection providing they are in the matching format, discussed more later. Inserting a Document let's you write JSON code for inserting additional collection data.
Click on "Add Data" and then choose "Insert Document".
Type:
{"title": "Hamlet"}on line 4 and click
INSERT
.
By Mousing Over each book you are presented with options to EDIT
, COPY
, CLONE
, or DELETE
each book.
This screen also allows you th search, filter, navigate through and refresh the data shown. This is a good data visualization tool for small datasets and reasonable for larger sets.
Click on "Local" in the top left corner to return to the list of databases.
In order to delete a database, you can click on the trashcan icon on the right side of each database. It is highly recommended that you do not delete the admin, config or local databases.
Delete the recently created "library2" database. As a safety precaution, it will make you type in the name of the database before it is dropped as the drop is permanent.
This concludes the installation and setup of MongoDB section as well as a quick intro to Compass.