MongoDB - The Basics

MongoDB
Database
Author

Stephen Barrie

Published

November 10, 2023

MongoDB

If you are conversant with Python and prefer the JSON format to the tabular data (rows and columns) approach used by SQL then MongoDB might be just what you have been looking for.

Unlike relational database management systems MongoDB uses a NO (Not Only) SQL format to store and retrieve data.

NoSQL.PNG

collections.PNG

MongoDB architecture

Data is grouped together in documents which comprise key:<value> pairs (like a Python dictionary) :

image.png

A collectionis a group of one or more documents :

collection.PNG

A database is a group of one or more collections :

database.PNG

Installing MongoDB (and the Compass GUI)

https://www.mongodb.com/

resources_server.PNG

install_windows.PNG

download.PNG

MongoDB comes with Compass which is a graphical user interface (GUI) which provides a visual alternative to the shell.

compass.PNG

Installing the MongoDB shell (mongosh)

shell_install.PNG

download_shell.PNG

mongo_shell_download.PNG

Once downloaded extract the files from the zip file and copy the file path as indicated in the graphic below:

copy_file_path.PNG

We will need to copy this file path to our environment variables for :

system_env_variables.PNG

vscode_path.PNG

Then, go back to the folder and open the executable. To establish a connection type mongosh into the console:

connection.PNG

You can clear the screen using the cls command. Refer to the documentation for a complete guide to the available commands.

Using the shell within Visual Studio Code

vscode_ext.PNG

Once installed click on the MongoDB extension icon (leaf) and initially you may have to copy the connection url into the search bar. In my case:

 mongodb://127.0.0.1:27017/mongodb?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2

launch_shell.PNG

vscode.PNG

Creating and dropping databases in the shell

create_drop_dbases.PNG

Inserting records

insert.PNG

compass_insert.PNG

.sort() - equivalent to ORDER BY clause in SQL

Ascending order

sort_asc.PNG

Descending order

sort_desc.PNG

.limit() - equivalent to LIMIT clause in SQL)

We can use this to quantify how many documents we wish to be returned. For example if we wish to find the student with the highest gpa score, we first sort by gpa in descending order (-1) and specify a limit value of 1 :

limit.PNG

.find( {query}, {projection}) - equivalent to the WHERE clause in SQL

find.PNG

In the above example we only specified the {query} parameter, however we can also include a {projection} parameter to specify which fields to return. For example, say we want the names of all of our students. The first parameter {} will give us all records, whilst the second parameter specifies that we only want the name fields :

query_projection.PNG

.updateOne({filter}, {update})

The $set operator replaces the value of a field. Say for example we wish to update Jerry’s record to include an address :

update.PNG

Updating by unique ObjectId

What if we have more than one student named Jerry? Well, we don’t want to update the records for all students named Jerry, we can use the unique identifier ObjectId :

object_id.PNGdownload.png

Removing fields

To remove a field we can use the $unset operator and set the new value as an empty string "":

remove_field.PNG

The fullTime field has been successfully removed.

updateMany({filter}, {update})

Say we wanted to set every student’s fullTime status to False, or True if the field doesn’t exist yet):

update_many.PNG

Exporting

Before we go ahead and show how to delete a collection let’s first back it up by exporting :

export.PNG

Deleting documents

.deleteOne()

Say we wanted to remove Kramer (I can’t imagine why!) from the database :

kramer_removed.PNG

.deleteMany()

Let’s delete any one with fullTime status of false :

jerry_removed.PNG

Just like that Jerry is gone!

As another example let’s delete any documents which don’t have a registerDate field (in our case this means everyone) :

all_removed.PNG

Importing

Let’s restore our data from the backup :

import.PNG

Comparison Operators

Not equal to $ne

not_equal.PNG

less than $lt

less than or equal to $lte

less_than_or_equalto.PNG

greater than $gt

greater than or equal to $gte

greater_than_equalto.PNG

between

between.PNG

is in $in [" ", " " ]

is not in $nin [" ", " " ]

in_notin.PNG

Logical Operators

And $and: ["{ }", "{ }"]

logical_op_and.PNG

Or $or: ["{ }", "{ }"]

logical_op_or.PNG

not $not

For example, say we wanted to find all students under 30, we could construct the query using $lt:30 but this would not return null values. If we want to include null values we should instead use the $not operator :

not.PNG

Indexes

indexes.PNG

b_tree.PNG

A linear search is going to take a really long time if there are hundreds of thousands or millions of documents to search through.

no_index_execution_stats.PNG

We can speed the look up process by applying an index.

index_execution_stats.PNG

Getting and dropping indexes

get_drop_indexes.PNG

For a more detailed discussion on the subject of MongoDB indexes see this Allegro Tech Blog post.

Collections

.createCollection("<collection_name>", {capped:true, size:#bytes, max:#documents}, {autoIndexId:false})

create_show_collections.PNG