Share
Explore

MONGO and MONGOOSE Lab Workbook



Learning Outcomes:

Use Node.js and MONGOOSE to make and populate data into a MONGO database.
Theme the examples on Pokemon for collections and documents.
MongoDB, which uses BSON (Binary JSON) for storing documents, was developed by the company MongoDB Inc. (formerly known as 10gen). The company was co-founded by Dwight Merriman, Eliot Horowitz, and Kevin Ryan in 2007. MongoDB was released to the public in 2009 as an open-source document-oriented database designed to improve scalability and performance compared to traditional relational databases.
The JSON-like documents in MongoDB allow for a schema-less or flexible schema design, which means that the documents in a collection do not need to have the same set of fields or structure, and common fields in a collection's documents can hold different types of data.
Eliot Horowitz, one of the co-founders, is particularly noted for his role in the creation and development of MongoDB, serving as the CTO of MongoDB Inc. until 2020. He was very much involved in the architectural decisions and the development of MongoDB.

Introductory Drills in MongoDB Compass for New Learners

Here are six introductory drills designed for new users to familiarize themselves with MongoDB Compass and basic CRUD (Create, Read, Update, Delete) operations on a MongoDB database.

Pre-Drill Setup

Before starting the drills:
Download and install MongoDB Compass.
Connect MongoDB Compass to a local MongoDB instance or Atlas cluster with the appropriate connection string.

Drill 1: Creating a Database and a Collection

Objectives:
Learn how to create a new database.
Learn how to create a collection within the database.
Actions:
Click the "Create Database" button on the Databases tab.
Enter learning_mongo as the Database Name.
Enter users as the Collection Name.
Click "Create Database."

Drill 2: Inserting Documents into a Collection

Objectives:
Insert a single document into your collection.
Insert multiple documents into your collection.
Actions:
Navigate to your learning_mongo database and users collection.
Click the "Insert Document" button.
In the opened editor, insert a single document with fields such as name, email, and age.
Example:

{
"name": "John Doe",
"email": "john.doe@example.com",
"age": 28
}
Click "Insert".
Insert multiple documents by clicking "Insert Document" again, adding a few more user entries in the array.

Drill 3: Finding Documents in a Collection

Objectives:
Learn how to use the find operation.
Learn to use queries to filter results.
Actions:
Click on the "Find" tab in your users collection.
Try a blank find query {} to retrieve all documents.
Execute a find query with a condition, like finding users with an age greater than 25 using {"age": {$gt: 25}}.

Drill 4: Updating Documents

Objectives:
Use the update operation to modify an existing document.
Learn to use update operators like $set and $unset.
Actions:
Find a user with the name "John Doe".
Click on the "Edit Document" button of the found document.
Modify John's age to 30 using the $set operator: {"$set": {"age": 30}}.
Update the user's document by removing the email field using the $unset operator.

Drill 5: Deleting Documents

Objectives:
Learn to delete a single document.
Learn to delete multiple documents matching a filter.
Actions:
Delete the previously updated John Doe using its unique _id by clicking on the "Delete Document" button.
To delete multiple documents, use the filter { "age": {"$lt": 18}} to match all documents representing users under 18, and click "Delete Documents".

Drill 6: Creating Indexes to Speed up Queries

Objectives:
Learn how to create an index on a field to improve query performance.
Actions:
Click on the "Indexes" tab while in your users collection.
Create a new index on the email field, which you expect to query frequently.
Use { "email": 1 } to create an ascending index.

By completing these six drills, new learners will become accustomed to the basic functions of MongoDB Compass and understand how to perform simple CRUD operations on a MongoDB database.
As you progress, they can tackle more complex queries, aggregation operations, and explore schema validation rules to complement your MongoDB skills.

No SQL databases: MONGO DB and JSON Schema data stores.

Introduce JSON databases.

Mongo DB: The emerging database technology.
SQL databases: Old School: 1972: Codd’s Laws: sql → Normalization achieved with the primary key is about eliminating duplication and simplifying our view of the data model.

An implication of Normalization (making everything look the same): is that you lose information about the business domain that you are modeling.
JSON is a much finer brush with which to paint the subtle details of the interactions between system components into our data model.
An application: Selling online advertizing:
Users of online platforms are put into buckets called demographic profiles.

1. Differences between SQL and Mongo (JSON, No SQL)
2. The role of the Primary Key.
3. The big wins of JSON databases:
Build LLM (large language model): Why we need JSON to create AI-centric information Solutions.
Activity 1: Install MONGO DB on your computers.
Activity 2: How to do sql stuff in JSON.
What is SQL stuff:
CRUD : Create Read Update Delete Records.
In addition to CRUD, what is the other big Thing we need to do in SQL?
We need to be able to make PREDICATE JOINS which are collections between tables.
How do we make Predicate JOINS between JSON collections?
A predicate JOIN in JSON database is done with embedded sub documents.
The Big Win?
Many to Many relationships in SQL : 1 relator table per each many:many relationship.
Another big win:
In SQL schema: the database structure is poured in cement.
JSON is just text: easy change the shape of the data container, programming, under program code control.
JavaScript Object Notation: A box into which we can put JSON data records.
A table in SQL is make up of Rowsets : a collection of columns which describe a unique entity (data point) in a Table.
Here in Json land: Rowsets are JSON Documents. KEY:Value Pairs.
Every data entity in JSON data schema is a Document.
Documents are stored in Collections. Also sets of Key:Value pairs.
Tables in SQL --> Collections.

Introduction

Welcome to the Node.js and Mongoose lab workbook! In this workbook, you will learn how to use Node.js and Mongoose to make and populate data into a MongoDB database. We will be using Pokemon as our theme for collections and documents.

Prerequisites

Mongo and its supporting toolsets are JavaScript Programs.
Therefore we will get those packages from the Node.js Code repository called

Before we get started, make sure you have the following installed on your machine:
Postman
You must node.js → provides the JavaScript code execution engine for the code to make and run our Mongo Databases.
How to verify that Node.js is installed:
Open a command prompt and type : Node
We also need a JavaScript Code IDE (integrated developed environment) : Visual Studio Code:

Download and install Mongo Database Community Edition:

Setting up the Project

Create a new directory for your project and navigate to it in your terminal. (Wednesday Lab Test 1)
Initialize a new Node.js project by running npm init -y.
Install the necessary dependencies by running npm install express mongoose.
Create a new file called server.js and add the following code:

const express = require("express");
const mongoose = require("mongoose");
const app = express();

mongoose
.connect("mongodb://localhost/pokemon", { useNewUrlParser: true })
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log(err));

app.listen(3000, () => console.log("Server listening on port 3000"));


This mongo code sets up a basic Express server and connects to a MongoDB database named "pokemon".

Creating a Model

Create a new file called pokemon.js in a directory called models.
Add the following code:

Lecture Notes: Mongoose Schemas

In MongoDB, data is stored in collections.
A collection is a group of documents, which are JSON-like objects that contain data.
Mongoose is an Object Data Modeling (ODM) library for MongoDB and provides Node.js APIs to define schemas to model the data in collections.

What is a Schema?

A schema is a blueprint for a collection. It defines the structure of the documents in the collection, including the field names, types, and any validation constraints.
In Mongoose, a schema is defined as a JavaScript object with key-value pairs that represent the fields in the collection.
const mongoose = require('mongoose');

const pokemonSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
type: {
type: String,
required: true
},
level: {
type: Number,
required: true
}
});

In this example, we define a schema for a Pokemon collection, which has three fields: name, type, and level. The type key specifies the data type of the field, and the required key specifies whether the field is required.

How are Schemas Used?

Once a schema is defined, it can be used to create a model.
A model is a JavaScript class that represents a collection and provides an interface for interacting with the data in the collection. To create a model, we pass the schema to the mongoose.model() method.
const Pokemon = mongoose.model('Pokemon', pokemonSchema);


CRUD:

In this example, we create a model for the Pokemon collection using the pokemonSchema. Now we can use the Pokemon model to perform operations on the collection, such as creating, reading, updating, and deleting documents.
const pokemon = new Pokemon({
name: 'Pikachu',
type: 'Electric',
level: 10
});
pokemon.save();

In this example, we create a new document in the Pokemon collection using the Pokemon model.
We pass an object with the field values to the Pokemon constructor, and then call the save() method to save the document to the collection.

Why are Schemas Used?

Schemas provide a way to enforce data consistency and validation when working with MongoDB.
By defining a schema, we can ensure that all documents in a collection have the same structure and that fields have the correct data types and validation constraints.
This helps to prevent errors and ensure that the data is consistent and accurate.
In addition, schemas provide a way to define methods and statics on models.
Methods are functions that operate on individual documents, while statics are functions that operate on the entire collection.
These methods and statics can be used to perform complex operations on the data in the collection and provide a more powerful interface for working with MongoDB.
In conclusion, Mongoose schemas are a powerful tool for modeling data in MongoDB collections.
They provide a way to enforce data consistency and validation, define methods and statics on models, and provide a powerful interface for working with MongoDB.
const mongoose = require('mongoose');

const pokemonSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
type: {
type: String,
required: true
},
level: {
type: Number,
required: true
}
});

const Pokemon = mongoose.model('Pokemon', pokemonSchema);

(async () => {
try {
await mongoose.connect('mongodb://localhost:27017/pokemonDB', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('Connected to database!');

const pokemonData = [
{
name: 'Pikachu',
type: 'Electric',
level: 10
},
{
name: 'Charmander',
type: 'Fire',
level: 7
},
{
name: 'Squirtle',
type: 'Water',
level: 5
}
];
await Pokemon.insertMany(pokemonData);
console.log('Data inserted!');
} catch (error) {
console.error(error);
} finally {
await mongoose.disconnect();
console.log('Disconnected from database!');
}
})();


This code creates a Mongoose schema for Pokemon with three fields: name, type, and level.

Creating Routes

Create a new file called pokemon.js in a directory called routes.
Add the following code:
const express = require("express");
const router = express.Router();
const Pokemon = require("../models/pokemon");

router.get("/", async (req, res) => {
const pokemon = await Pokemon.find();
res.send(pokemon);
});

router.post("/", async (req, res) => {
const pokemon = new Pokemon({
name: req.body.name,
type: req.body.type,
level: req.body.level,
});
await pokemon.save();
res.send(pokemon);
});

module.exports = router;

This code creates two routes for getting all Pokemon and creating a new Pokemon. The async and await keywords are used to handle asynchronous operations.

Testing the Routes

Start your server by running node server.js.
Open Postman and create a GET request to http://localhost:3000/pokemon. You should see an empty array.
Create a POST request to http://localhost:3000/pokemon with the following JSON body:
{
"name": "Pikachu",
"type": "Electric",
"level": 10
}


Send the request and you should see the new Pokemon in the response.

Conclusion

Congratulations! You have successfully learned how to use Node.js and Mongoose to make and populate data into a MongoDB database using Pokemon as our theme.
Use this knowledge to build your own applications and explore the vast possibilities of Node.js and MongoDB.


Lab Workbook: Testing a Node.js Server with Postman

In this lab, you will learn how to use Postman to test a Node.js server that you have built. We will be using a server that has been built using the Express.js framework and Mongoose to interact with a MongoDB database.

Prerequisites

Node.js and NPM installed on your computer
Postman installed on your computer
Basic knowledge of HTTP requests and responses

Setting up the Server

Put your server code in server.js using established methods.
Install the dependencies using npm install
Start the server using npm start

Testing the Server with Postman


Open Postman and create a new request.
In the request URL field, enter http://localhost:3000/api/pokemon.
Select the POST HTTP method.
Click on the Body tab and select raw as the input format.
Enter the following JSON data in the input field:

{
"name": "Pikachu",
"type": "Electric",
"level": 10
}

Click on the Send button to send the request.
You should receive a response from the server with the newly created Pokemon document. You can repeat this process for other HTTP methods like GET, PUT, and DELETE.

How to use postman

Postman is an API client that allows developers to create, test, and document APIs. It provides a user-friendly interface for sending HTTP requests and viewing responses. Here are the basic steps to use Postman:
1. Download and install Postman: Postman can be downloaded from the official website for free.
2. Create a new request: To create a new request, click on the "New" button in the top left corner of the Postman window. Select the type of request you want to send (GET, POST, PUT, DELETE, etc.) and enter the URL of the API endpoint you want to test.
3. Add parameters and headers: If your API requires parameters or headers, you can add them by clicking on the "Params" or "Headers" tabs in the request builder.
4. Send the request: Once you have set up your request, click on the "Send" button to send it to the server.
5. View response: The response from the server will be displayed in the "Response" tab below the request builder.
6. Save requests: You can save your requests for future use by clicking on "Save" button in top right corner of Postman window.
7. Organize requests into collections: You can organize your saved requests into collections for easy access by clicking on "Collections" tab in left side bar of Postman window.
8. Test automation with Postman Monitors: You can automate testing with Postman Monitors which give continuous visibility into health and performance of APIs

Postman also provides many other features such as environment variables, scripts, testing suites, etc., which make it a powerful tool for API development and testing

Conclusion

Congratulations! You have successfully learned how to use Postman to test a Node.js server. Use this knowledge to test your own servers and APIs and ensure they are working correctly.
More on using Postman:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.