A basic MongoDB example with instructions to install MongoDB, load data into the database and then query it back. This is extended from the Aggregation with the Zip Code Data Set example on the MongoDB website.
TIP: This is a good project to start with you if you are just starting to learn about MongoDB and are already familiar with JavaScript.
Follow these instructions to execute the example. As a pre-requisite, you must first install MongoDB. See the Installing MongoDB section for options.
- Start the MongoDB server:
- The start command depends on whether you are using Docker or not. Follow the Installing MongoDB section as a pre-requisite to this step and learn how to run a MongoDB server.
- Load the database with ZIP Code test data from the states of Georgia (GA) and Montana (MT):
-
mongoimport --db test --collection zips zips-GA.json mongoimport --db test --collection zips zips-MT.json
-
- Start a shell session:
-
mongosh
-
- Switch to the
test
database (this is the database with the ZIP Code data):-
use test
-
- Query the size of the
zips
collection:-
db.zips.find().size()
- Success, you've written and executed your first query!
- Exit the the shell session with
exit
-
- Query the average population of ZIP areas by city using the pre-defined query in
zips-average-population.js
:-
mongosh --quiet zips-average-population.js
- Success, you've executed your first complex query!
-
- Stop the MongoDB server
There are various options for getting up and running with MongoDB locally. The most direct way is to download MongoDB
from the official website (see the relevant link under Reference) or via HomeBrew. You
should then follow the additional documentation on the MongoDB site to learn how to create a data directory, start the
MongoDB server, start an interactive shell session using the mongo
command, and stop the MongoDB server. You should also
install the separate package known as MongoDB Database Tools.
An alternative option is to run a MongoDB server using Docker. There is a docker-compose.yml
file in this project to help
accomplish that. Using the Docker option, you can do the following:
- Start a MongoDB server:
-
docker-compose up --detach
-
- Enter an interactive
mongo
shell session:-
docker exec -it basic-mongo-1 mongo
- NOTE: while this option is clever, it would pay off to install Mongo so that you can use the
mongo
command directly to start a shell session instead of this extra-ness of starting a session via the Docker container. The strength of the "Mongo-via-Docker" option is in running the server and handling the data directory in a convenient way to the user. Whereas starting a shell session (client) is more convenient to do via themongo
command compared to the long-windeddocker exec...
alternative command.
-
- Stop the server:
-
docker-compose down
-
- MongoDB: download Community Server
- MongoDB: Installing the Database Tools on macOS
- MongoDB: run the
mongod
process - MongoDB: Aggregation with the Zip Code Data Set tutorial
- MongoDB: Write Scripts for the
mongo
Shell`
General clean-ups, TODOs and things I wish to implement for this project:
- DONE Make a monolithic query in
zips-average-population.js
instead of using the intermediate collections. This is a more approachable, minimal example appropriate for a basic MongoDB example project.