M001 - MongoDB Basics Notes

Author post avatar

Dante Calderón | January 20, 2022

M001 - MongoDB Basics Notes cover

Chapter 1: What is MongoDB?

MongoDB is a NoSQL database.

A NoSQL Database is a database that is structured and can be accessed in a different way to SQL databases, it doesn’t use a legacy approach of related tables of data, not in rows and columns.

  • The data in MongoDB DB is stored in Documents.
  • Documents are stored in Collections.

MongoDB actors
MongoDB actors

MongoDB actors

What is a Document?

Is a way to organize and store data as a set of field-value pairs.

Field - a unique identifier for a data point.

Value - data related to a given identifier.

11369bd2 12fb 43d4 8e21 448eef5c7913 Untitled 988d565a

What is a Collection?

An organized store of documents in MongoDB, usually with common fields between documents.

There can be many collections per-database and many documents per collection.

54866520 98f9 4978 bac4 e24b8971acc1 Untitled c2755fcb

What is MongoDB Atlas?

Atlas is a cloud service that provides a fully managed database built for a range-wide of applications with MongoDB at its core.

It allows you to visualize, export, and more.

76751fdf bd93 455b 9060 d3219325984b Untitled d5a96f96

Cluster Deployment

Atlas users can deploy Clusters.

Cluster - group of servers that store your data.

These Clusters are configured in Replica Set.

On the left a Cluster, on the right a Replica Set
On the left a Cluster, on the right a Replica Set

On the left a Cluster, on the right a Replica Set

Replica Set - a few connected MongoDB instances that store the same data to ensure that if something happens to one of the machines the data will remain intact. Comes from the word replicate - to copy something.

The major difference between a replica set and a cluster is:

  • A replica set copies the data set as a whole.
  • A cluster distributes the workload and stores pieces of data (shards) across multiple servers.

MongoDB allows users to combine these two functionalities by creating a sharded cluster, where each shard is replicated to a secondary server in order to provide high data availability and redundancy.

Instance - a single machine locally or in the cloud, running certain software, in our case it is the MongoDB database.

An Instance is a single machine
An Instance is a single machine

An Instance is a single machine

This setup ensures that if something happened to one of the machines in the Replica Set, the data will remain intact and available for use by the application by the remaining working member.

One of the machines in the Replica Set is failing
One of the machines in the Replica Set is failing

One of the machines in the Replica Set is failing

Every time that you make a change to a document or collection, redundant copies of that data are stored within the replica set.

It means a copy of that document will exist on every machine in the Replica Set.

ℹ️ To go deeper about ReplicaSet and Clusters, you can check this answer in StackOverflow: What is the difference between a cluster and a replica set in MongoDB atlas?

Services

  • Managed cluster creation
  • Run and maintain database deployment
  • Use the cloud provider of your choice
  • Experiment with new tools and features

Pricing

708146c0 bea7 4c64 b356 c309ac379f06 Untitled 39d2cc80

Some free tier features

4fffcd58 6ac7 4be6 88f0 bc662aa1e797 Untitled a14e0ae3

Useful Links:

Chapter 2: Importing, Exporting, and Querying Data

How documents are represented?

Documents are saved as BSON which is a binary representation of JSON.

A document JSON
A document JSON

A document JSON

JSON is:

  • User friendly
  • Readable
  • Familiar

Cons of JSON:

  • Text-based
  • Space Consuming
  • Limited number of data types.

BSON is optimized for:

  • Speed
  • Space
  • Flexibility
  • High performance
  • General-purpose focus

JSON vs BSON
JSON vs BSON

JSON vs BSON

The additional types in BSON allow for easing communication.

MongoDB stores data in BSON internally and over the network, but this doesn’t mean you can’t think of MongoDB as a JSON Database.

Anything you can represent in JSON can also be stored in MongoDB.

We can retrieve and store data as JSON. Actually, with native SDK always use JSON, we don’t think about BSON, it’s just an internal feature that in our day-to-day we never think of.

Optional reading:

Importing and Exporting Data

Data is Stored in BSON but Viewed in JSON
BSON
is great but isn’t human-readable

To learn more about other mongoimport supported formats check out this documentation page.

SRV connection string - a specific format used to establish a connection between your application and a MongoDB instance. Click here to learn more.

Code used in this lecture:

mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"

mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --collection=sales --out=sales.json

mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"  --drop dump

mongoimport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop sales.json

Terminal Commands

fa1d0097 085a 471a b01d ffac625056d4 Untitled 8d23c729

Exporting

9c43a443 ba49 498c 8525 0dd2734e3179 Untitled a08ff603

Importing

c7ec50a1 e206 40b7 8ed8 05d1f1c52c67 Untitled bd39ed0b

Data Explorer

mongosh allows you to interact with your MongoDB instance without using a Graphical User Interface

2b89faf6 cf9c 4ebf 9ba6 8633ea40da55 Untitled 7041e403

Chapter 3: Creating and Manipulating Documents

Inserting New Documents - ObjectId

Every document must have a unique _id value.

Optional Reading

If you want to read more about the ObjectId data type, and the ObjectId() function, which generates ObjectId values, check out this excellent documentation page.

Every document can have a different structure, nothing prevents you from doing this. Either is a good practice to know how to organize your data.

The default value of _id field is ObjectId() unless otherwise is specified.

Idential documents can exist in the same collection as long as their _id values are different.

MongoDB has schema validation functionality allow you to enforce document structure.

Inserting New Documents - insert() order

Insert in order, which means if something fails, it will fail and will not continue with the next item.

db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 },{ "_id": 3, "test": 3 }])

We can avoid this behaviour by adding the option { ordered: false

db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 }, { "_id": 3, "test": 3 }], { ordered: false } )

Updating Documents - mongo shell

0c1fceff ff76 4e68 929d 0973eb055e1a Untitled e7ea293b

Deleting Documents and Collections

05d7e53a a913 40c0 8258 b43d3ccacc0a Untitled 65c67bd2

We can delete a collection by doing:

db.collection_name.drop()

When we delete all collections from a database, the database will be removed as well.

Update operators

Example: $inc $set $unset

Chapter 4: Advanced CRUD Operations

Query Operators - Comparison

Query operators provide additional ways to locate data within the database

Comparison operators specifically allow us to find data within a certain range.

32d2067b e944 4b6e 80a0 12da4c6cb05c Untitled 04e3c0f2

If a comparison operator is not specified, $eq is used as default operator.

Check the list of comparison operators here.

Query Operators - Logic

00e2c5ac 8da2 4d21 89e5 619d5414fa94 Untitled f32b9d4d

Structure:

fc42d20d 91dc 4975 9452 7fa559246c70 Untitled 11d329c5

And

And is implicit:

27d5fae1 c585 464c a316 f25397be7611 Untitled 303beba2

Here, and is implicit.

5dfb2f1d 4207 40a7 a31f e9de1a57fcb2 Untitled aebdffa3

04fb5a2a d6ed 4ca3 a163 65a10745be38 Untitled dd50976a

Expressive Query Operator

$ denotes the use of an operator

$ addresses the field value

34d4d2c6 1f2c 4f91 a4b0 d09c5d68abea Untitled 4642e3e5

Class: https://university.mongodb.com/mercury/M001/2022_January_18/chapter/Chapter_4_Advanced_CRUD_Operations/lesson/5f36f78304e9ffa9eb9deb79/lecture

52efbfef e1ad 4b19 bf13 1f14077cb9b7 Untitled cc3fbf8f

4ea8c273 ff64 4b25 8e4e 58d3232704ce Untitled 891a1ea1

Array Operators

ed981005 a079 46ec 88d0 46acb3972a2d Untitled ce201b6e

73c7686a fbdc 43fc a276 22762aca59d3 Untitled 1f5aca0e

Array Operators and Projection

41b874cd 65b4 4ddd af51 60e9133916b4 Untitled abfdc7c5

Chapter 5: Indexing and Aggregation Pipeline

Another way to create queries.

36460ebd ae69 4795 ace5 d64c8c0ec909 Untitled 345220b6

d6be69a9 4070 4c2a 8814 dc26b422cfc5 Untitled 627e93d1

Get into the bus at 1st St. then

$group

71d2fa58 ee01 4025 8c81 87c13934911d Untitled 2fb3a4d6

Recap:

1051b40e 354f 467b 9060 49126727f481 Untitled 0bdcf368

sort() and limit()

sort and limit are cursor methods.

A cursor method is not applied to the data in the database, instead applied to the results in the cursor after find command.

You can sort in increasing or decreasing order:

6b4c9f31 1260 4ffc 9950 ed8efc22c77f Untitled 7690bbcb

92f82404 ada7 4ebc 80e1 a7298b9d9dc0 Untitled 2d95640d

Introduction to Indexes

Introduction to Data Modeling

Data modeling - a way to organize fields in a document to support your application performance and querying capabilities.

To learn more about data modeling with MongoDB, take our Data Modeling Course! Check out our documentation and blog.

4f9ed074 d03b 4c57 95d0 05b16e8b1d8e Untitled 352baa14

Upsert - Update or Insert?

01fbc0d6 4986 4891 9f51 e3ddbf48bb21 Untitled 7a9ebb02

57756f8c 2f8c 4151 b56d b09898754855 Untitled b92fc534

Aggregation Framework

1. Using the aggregation framework find all documents that have Wifi as one
   of the amenities. Only include price and address in the resulting cursor.

2. Which countries have listings in the sample_airbnb database?
3. How many countries have listings in the sample_airbnb database?

sort() and limit()

1. Find the least populated ZIP code in the zips collection.
2. Find the most populated ZIP code in the zips collection.
3. Find the top ten most populated ZIP codes.
4. Get results sorted in increasing order by population, and decreasing
   order by city name.

Introduction to Indexes

Create two separate indxes to support the following queries:

db.trips.find({"birth year": 1989})

db.trips.find({"start station id": 476}).sort("birth year": 1)

89c61be0 e1e2 44c2 8f6b 43cad666ab2d Untitled 4df6f2aa

Documentation about the Validation tab in Compass.

Schema Validation with MongoDB Documentation.

JSON Schema Validation - Locking down your model the smart way.

JSON Schema Validation - Checking Your Arrays

Why MongoDB? (optional)

At this point you have learned enough to start having a meaningful conversation about why developers, DBAs, and companies choose MongoDB as their database.

This can also help you decide whether MongoDB is the right choice for you.

To help you learn more about specific use cases, we put together case studies and articles about some of our customers and community members.

We also added a link to the MongoDB Developer Hub and Community Forums where you can ask questions, learn new things, and connect with others who are diving into MongoDB content and products.

Take a look and feel free to browse for more information.


TAGS: 📝 Notes🌿 MongoDB
Author post avatar

Written by

Dante Calderón

Hi, I'm Dante Calderón, web developer. Click here if you want know more about me or checkout my portafolio.