Intro
We're going to start talking about how to actually use MongoDB to build and manipulate databases. In the previous discussion, we used the MongoDB Shell to build and manipulate our database, and in this discussion, we'll show you how to use MongoDB Compass to build our database.
We are going to be creating a database for an Animal Shelter. This database could hold information on different pets such as dogs and cats, and also perhaps, people that may be looking for pets. But, the first thing we do is to create the new database.
Once you have a database created, we will create what are called Collections to store and reference the data. Think of collections as basically different categories of data that are stored in the database.
And finally, once you have a database, with at least one Collection, you then create what are called Documents. Documents are, in the end, the actual data being stored.
When we installed MongoDB, we also used to option to install MongoDB Compass which is a Graphical User Interface (GUI) for MongoDB. And, as with most gui's, it is much simpler to use the Compass GUI for the building and manipulating of our databases.
More information on all of the CRUD (Create/Read/Update/Delete) operations can be found on the MongoDB Docs Page.
Compass GUI
Create the Database
We're going to be creating a database for an Animal Shelter, so the first thing we need to do is to create a new database:
- Start the Compass App
- Connect to our localhost server - default port is 27017
- Create a new database - just to the right of "localhost:27017" click the + sign
- Name your database - animalShelter
- Name your first collection (of data)
- this step is optional because you can create new "collections" at any time by clicking the + sign just to the right of the database name.
Create a Collection
As already mentioned, MongoDB utilizes "Collections" to store it's data. So once we have created our new database, we need to add our first collection. We're going to create a collection of dogs that will refer to any dogs we have in our shelter:
- Click the + sign next to our DB Name (animalShelter) in the DB List
- Name your collection (dogs)
and yes, it's that simple
Create a Document
And now we'll add our first set of data, which Mongo calls a "Document". Start by clicking on the green dropdown box to Add Data. From the dropdown, you can either Import data or Insert an empty document which opens a template to enter our data. We will choose to Insert. When we add the new data, which in this case is going to refer to one specific dog, we want to add it in json format.
In the empty document template, you'll notice that Mongo has automatically added a unique ID number to identify this dataset. This is the default behavior for Mongo. But we want to add our data first, so let's delete everything inside. Not to worry about the ID, Mongo will add it when we are done.
One quick note about the ID numbers. You can generate your own ID's if you wish. Mongo will only generate an ID if one does not already exist.
Next we just need to insert the following data (in json format) for our first dog:
- {
- "name": 'Chico',
- "age": 10,
- "breed": 'Pit Bull',
- "catFriendly": true
- }
and press the big green "Insert" button when we are finished. We have just added our first Document to our dogs collection. We can also add multiple "documents" to our collection, at one time by nesting them in an array structure, like so:
And now, if we go back to the main Compass page and select our dogs database, we see our new Document added to our new collection, complete with it's unique ID number. Remember that Mongo will automatically add a unique ID number if one does not already exist.
Read from a Collection
So we now know how to create a database, create collections within the database, and add Documents to a Collection. Next we're going to learn how to Read some data from a database. Remember that we are working within the CRUD framework which stands for Create/Read/Update/Delete.
To Read from a Collection in Compass, we just have to select the database we want to search, enter the search criteria in the search box, and press the green Find button.
Update/Delete a Document
To Update or Delete a document in Compass is very easy. Just pull up the Collection with the Document(s) you want to Update or Delete. In the Top/Right corner of the documents view is a row of buttons. Just click on the appropriate button to perform the task you would like.