By the end of this, developers should be able to:
- Interact with MongoDB databases and collections using the MongoDB shell.
- Create, Read, Update, and Delete documents in MongoDB collections using the MongoDB shell.
- Fork and clone this repository
- Create a new branch,
training
, for your work. - Checkout to the
training
branch. - Install dependencies with
npm install
.
Relational databases are good at modeling data that fits into tables. What do you use if your data isn't that structured?
Perhaps a NoSQL data-store.
MongoDB is a schema-less document-store that organizes documents in collections. What does this mean?
Relational Database Term | MongoDB Term |
---|---|
database | database |
table | collection |
row | document |
column | field |
brew install mongodb
brew services restart mongodb
-
Ubuntu https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
-
Windows https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/
We'll use mongo-crud
as the database to hold our tables and
mongo to interact
with it. mongo
is MongoDB's command line client which lets us execute
commands interactively and from scripts.
First let's fire up our server:
- Mac OS only:
brew services start mongodb
- Ubuntu Only:
sudo service mongod start
- WSL Only:
sudo service mongodb start
The command to enter into the MongoDB shell is mongo <name-of-database>
:
$ mongo mongo-crud
MongoDB shell version v3.x.x
connecting to: mongo-crud
>
The command to display the current database in use is db
:
> db
mongo-crud
>
The command to list databases is show databases
(or show dbs
):
> show databases
local 0.000GB # or local 0.078GB
>
Unlike PostgreSQL, MongoDB lets us select a database that hasn't been created. When we add a collection, the database will be created.
For instance, if we didn't specify the database on the command line, we can
connect to a database with use <database-name>
:
$ mongo
MongoDB shell version v3.x.x
connecting to: test
> db
test
> use mongo-crud
switched to db mongo-crud
> db
mongo-crud
> show databases
local 0.000GB # or local 0.078GB
>
To help us practice interacting with a Mongo database, we'll want some data to
work with. MongoDB's mongoimport
command will let us load bulk data from a
JSON
or CSV
file.
Our first collection will be called people
. It has no entries.
> show collections
> db.people.count();
0
This is a common pattern in MongoDB: you can refer to things that don't yet exist, and it will cooperate. MongoDB won't create them until you give it something to remember.
Watch as I load data in bulk from data/books.csv
. We'll save the
command in practice-scripts/import/books.sh
.
mongoimport --db=mongo-crud --collection=books --type=csv --headerline --file=data/books.csv
First, we'll load data in bulk from data/people.csv
. We'll save the
command in practice-scripts/import/people.sh
.
mongoimport --db=mongo-crud --collection=people --type=csv --headerline --file=data/people.csv
If we want to clear the collection before the import, we pass the --drop
flag.
Run this script by typing:
sh path_to_file.sh
Now that we've inserted data into it, the mongo-crud
database and the people
collection both exist.
$ mongo mongo-crud
MongoDB shell version v3.x.x
connecting to: mongo-crud
> show dbs
local 0.000GB
mongo-crud 0.000GB
> show collections
people
> db.people.count();
2438
On your own, use mongoimport
to bulk load from data/doctors.csv
and
data/ingredients.csv
. Save the commands as .sh
files and run them from the
terminal (not the Mongo shell!).
- Querying
- Overview of retrieving data from MongoDB.
- Queries
- More detailed overview on retrieving data.
- Query Operators
find
- Detailed documentation on the
find
collection method.
- Detailed documentation on the
findOne
- Detailed documentation on the
findOne
collection method.
- Detailed documentation on the
- Data aggregation
- Overview of summarizing documents.
aggregate
- Detailed documentation on the
aggregate
collection method.
- Detailed documentation on the
MongoDB uses JSON natively (technically BSON), which makes it well suited for JavaScript applications. Conveniently, MongoDB lets us specify the JSON as a JavaScript object.
Let's see some of what we can learn about the books in the database.
> db.books.find({author: "Ernest Hemingway"}).pretty()
{
"_id" : ObjectId("583ee3f3e6ae0faa5547068e"),
"title" : "A Farewell to Arms",
"author" : "Ernest Hemingway",
"published_on" : "1986-02-15"
}
{
"_id" : ObjectId("583ee3f3e6ae0faa5547071a"),
"title" : "The Sun Also Rises",
"author" : "Ernest Hemingway",
"published_on" : "2002-10-20"
}
> db.books.find({published_on: /20/}).count()
36
> db.books.find({published_on: /20/}).sort({title: -1}).limit(2).pretty()
{
"_id" : ObjectId("583ee3f3e6ae0faa5547072f"),
"title" : "Wide Sargasso Sea",
"author" : "Jean Rhys",
"published_on" : "2011-01-14"
}
{
"_id" : ObjectId("583ee3f3e6ae0faa55470725"),
"title" : "Trader",
"author" : "Charles de Lint",
"published_on" : "2003-06-23"
}
Note: When using the REPL, the .pretty()
method can be quite helpful.
What do we see?
- MongoDB gave each of our documents a unique ID field, called _id.
- MongoDB doesn't care that some documents have fewer or more attributes.
Together, we'll build a query for our people collection. Let's see if we can find all people born after a date. How about the number of people under 5 feet tall? What about all the doctors who perform surgery?
Write a query to get all the ingredients with a unit of tbsp
.
- Removing Data
- Overview of removing documents from a collection.
remove
- Detailed documentation of MongoDB's
remove
collection method.
- Detailed documentation of MongoDB's
deleteOne
- Detailed documentation of MongoDB's
deleteOne
collection method.
- Detailed documentation of MongoDB's
deleteMany
- Detailed documentation of MongoDB's
deleteMany
collection method.
- Detailed documentation of MongoDB's
If we want to clean up, db.<collection>.drop();
drops the specified collection
and db.dropDatabase();
drops the current database.
We'll remove a few books from the data-store. There are methods for removing one entry and multiple entries.
> db.books.deleteOne({author: "John Irving"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.books.deleteMany({author: "Sinclair Lewis"})
{ "acknowledged" : true, "deletedCount" : 2 }
Let's remove all the people with a specific born_on
date and doctors with
Internal medicine
as their specialty.
Remove ingredients that have ml
as their unit of measure.
- Updating Data
- Overview of changing documents.
update
- Detailed documentation of MongoDB's
update
collection method.
- Detailed documentation of MongoDB's
- Update Operators
- The different modifications we can make during an update.
MongoDB makes it easy to add an array of items to a document. We'll update
some books and give them a correct published_on
value.
> db.books.update({title: 1984}, {$set: {published_on: "1949-06-08"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.books.update({title: "Slaughterhouse-Five"}, {$set: {published_on: "1969-03-31", book_cover: "brown", pages: 247} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
What happens if we run an update
command without the $set
option?
Now, let's update some people with a hometown. Let's update some doctors' specialties.
Update a couple of ingredients' units.
- Inserting data
- Overview of adding documents to a collection.
insert
- Detailed documentation of MongoDB's
insert
collection method.
- Detailed documentation of MongoDB's
Next, we'll use the insert
collection method to add a few more people. We'll
save our invocations in scripts/insert/people.js
. We'll execute that script
using the mongo
load
method. Let's give these people a middle_initial or a
nick_name. Note that the attributes we choose for these people need not match
those from the data we loaded in bulk.
> load('practice-scripts/insert/people.js');
Together we'll add a few doctors.
Add a few ingredients to the ingredients
collection using insert
.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].