NoSQL databases are key to ingesting unstructured data. In many cases, RESTful APIs provide unstructured data, and hence we need a way to store and analyze such data.

In recent years, NoSQL databases have features that help store data for AI, such as embeddings. This is not part of the course, but it is noteworthy if you are looking to dive into the world of AI in future.

There are many different types of NoSQL databases in the world, and MongoDB happens to be one of the more popular NoSQL databases around.

What does unstructured data mean?

  • In layman terms, the columns/variables are dynamic in nature.
    • For example, you could potentially query a collection (table in relational database) today and expect a slightly different data point tomorrow.
    • Because it has no restrictions outside of a primary and/or a sorting key, it is very easy to ingest such data via a NoSQL database.
    • However, data cleaning and processing becomes much more difficult since it is not easy to fit the data into Pandas dataframe.

Basic Mongo Queries

Let’s open up the file(s) in the 01-Ins_MongoBasics folder to get started.

How to run your MongoDB services within your local machine?

In both setup, you will need 2 terminal/git-bash windows. One will act as a server, the other will be a client that interacts with that server.

Mac
  • Be sure to install Mongodb through Homebrew, or it won’t work.
  • In the first window, type: brew services start mongodb-community@6.0
    • Try not to interfere with anything on this window since if you close it, you will terminate the MongoDB application.
  • In the second window, type: mongosh. This starts a prompt for you to interact with the MongoDB instance.
Windows
  • If you have followed all the instructions to use environment variables to run the instance, then type: mongod
  • Otherwise, type: "C:\Program Files\MongoDB\Server\6.0\bin\mongod.exe" --dbpath="c:\data\db"
    • Try not to interfere with anything on this window since if you close it, you will terminate the MongoDB application.
  • In the second window, type: mongosh.exe to start the client to interact with the server.

Exploring Queries

Make sure you’re in the Mongo shell before attempting.

To create/use a database, type: use database_name

  • Your shell prompt should show the database you’re in.
  • Example, type: use travel_db

To show all databases in your system, type: show dbs

  • If your database you’ve just created has no data, it will not show its name.

To show collections in the database, type: show collections

  • Collections are similar to tables. You can have many collections within a database.
  • To insert a document into a collection, type: db.collectionName.insert({key:value})
  • Example, type: db.destinations.insert({"continent":"Africa", "country":"Morocco", "major_cities":["Casablanca", "Fez", "Marrakech"]})
  • Go to https://jsonlint.com/, and copy-paste the JSON object for pretty-printing:
{
	"continent": "Africa",
	"country": "Morocco",
	"major_cities": ["Casablanca", "Fez", "Marrakech"]
}

To print out some data, type: db.collectionName.find().pretty() where collectionName is the name of your collection/table.

  • Example, after you’ve created travel_db as your database, destinations as your collection, and inserted a record above, type: db.destinations.find().pretty()
  • It will output in order that was stored.
  • .pretty() makes the output more human-friendly. You can try without that method.

To find specific documents based on their attributes, type: db.collectionName.find({key:value})

  • Example, after you’ve done the above steps, type: db.destinations.find({"continent":"Africa"})

Within the document, the primary key is generated by MongoDB for you: _id

  • You can specify primary keys when creating a document.

Students Do: Mongo Class

Removing, Updating, and Dropping in MongoDB

Let’s open up the file(s) in the 03-Ins_CrudMongo folder to get started.

Students Do: GardenDB

Importing Data, Accessing Nested Data, and Modifying Data Types

Let’s open up the file in the 05-Ins_ImportData folder to get started.

Students Do: Import, Update, and Explore

Mongo Compass

Download the app for your machine at https://www.mongodb.com/products/compass

  • I use the Community Edition, because MongoDB can be a paid product.
  • You need the mongod daemon to run. This means, everytime you want to start a session, go to your terminal/bash and type: mongod first. Leave that window open.
  • The default settings should allow you to connect the MongoDB Compass to your local machine. Connect at will.

Student Do: Compass Playground