M001: MongoDB Basics

Learn the fundamentals of MongoDB.

What is MongoDB?

A structured way to store and access data, that doesn't use legacy approach of related tables of data. So you store data in an organized way just not using tables rows and columns. MongoDB is a No-SQL document database.

What is a document?

{ "field" : "value", "field" : "value", "name" : "Lachshmi", "title" : "Team Lead", "age" : "26" }
  • Document - a way to organize and store data as a set of field-value pairs.
  • Field - a unique identifier for a datapoint.
  • Value - data related to a given identifier.
  • 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.

What is MongoDB Atlas?

Atlas has many tools and services within it that are built specifically for the MongoDB Database. Replica Set - a few connected machines 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. Instance - a single machine locally or in the cloud, running a certain software, in our case it is the MongoDB database. Cluster - group of servers that store your data.
Importing, Exporting, and Querying Data

More information on JSON VS. BSON

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

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

Atlas UI

Collections is the Data Explorer feature of Atlas. Quering is avalible in the collections find section and the filter search box.

MQL

Using the mongo shell we can complete atlas functions directly from the terminal. Interacting with the data is done by using MQL - MongoDB query language.

Terminal Commands

> show dbs - lits databases in current access > use database_name; - opens database > show collections; - lists collections within current database > db.collection.count() > db.collection.pretty() - Cleans returned query so its more readable > db.collection.find() > db.collection.updateMany() - updates matched queries

Update Operators

$inc - increments values $set - sets values & creates new field, if field does not exist $unset - deletes values

Comparison Operators

$eq - equal to / default operator $ne - Not equal to $gt - grater than $lt - less than $gte - greater than or equal to $lte - less than or equal to Syntax {"field" : {"$operator" : value}}

Logical Operators

$and - match all the specified query clauses / default operator when operator is not specified $or - at least one query clause is matched $nor - fail to match both given clauses $not - negates the query requirement {"$not" : {cluase} } Syntax {"$operator" : [ {cluase}, {cluase},…] }

Expressive Operator

$expr - allows the use of aggregation expressions within the query language {"$expr" : {expression} } - it also allows up to use variables and conditional statements
Indexing and Aggregation Pipeline

Next Steps