KEMBAR78
Module 4 | PDF | No Sql | Mongo Db
0% found this document useful (0 votes)
13 views105 pages

Module 4

This document provides an overview of MongoDB, a NoSQL database, highlighting its key features, types, and when to use it. It covers basic CRUD operations, sharding strategies, and the differences between MongoDB editions, along with installation instructions and data formats. The document also explains the mongo shell and projection in queries, making it a comprehensive guide for understanding and using MongoDB.

Uploaded by

surya rk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views105 pages

Module 4

This document provides an overview of MongoDB, a NoSQL database, highlighting its key features, types, and when to use it. It covers basic CRUD operations, sharding strategies, and the differences between MongoDB editions, along with installation instructions and data formats. The document also explains the mongo shell and projection in queries, making it a comprehensive guide for understanding and using MongoDB.

Uploaded by

surya rk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 105

Module-4

MongoDB and Building REST API using


MongoDB

IT – TE – Advanced Internet Programming


Lecture -27
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

1
Samundiswary S
Learning Objective

● To get familiar with concepts of MongoDB and Node.JS

2
Samundiswary S
NoSQL

● NoSQL databases are non-tabular databases and store data differently than
relational tables. NoSQL databases come in a variety of types based on their
data model. The main types are document, key-value, wide-column, and
graph. They provide flexible schemas and scale easily with large amounts of
data and high user loads.
NoSQL database features
● Each NoSQL database has its own unique features. At a high level, many
NoSQL databases have the following features:

● Flexible schemas
● Horizontal scaling
● Fast queries due to the data model
● Ease of use for developers

3
Samundiswary S
NoSQL
Types of NoSQL databases
● Over time, four major types of NoSQL databases emerged: document databases, key-
value databases, wide-column stores, and graph databases.
● Document databases store data in documents similar to JSON (JavaScript Object
Notation) objects. Each document contains pairs of fields and values. The values can
typically be a variety of types including things like strings, numbers, booleans, arrays,
or objects.
● Document-oriented NoSQL database solutions include MongoDB, CouchDB, Riak,
Amazon SimpleDB, and Lotus Notes
● Key-value databases are a simpler type of database where each item contains keys
and values.
● Key-value NoSQL solutions include Dynamo, Redis, Riak, Tokyo Cabinet/Tyrant,
Voldemort, Amazon SimpleDB, and Oracle BDB.
● Wide-column stores store data in tables, rows, and dynamic columns.
● Examples of column-based NoSQL databases include Cassandra, HBase, and
Hypertable.
● Graph databases store data in nodes and edges. Nodes typically store information
about people, places, and things, while edges store information about the relationships
between the nodes.
● Graph-based NoSQL database solutions include Neo4J, Infinite Graph, and FlockDB.

● https://www.spiceworks.com/tech/artificial-intelligence/articles/what-is-nosql/

4
Samundiswary S
When to use NoSQL

When deciding which database to use, decision-makers typically find one or more of the
following factors lead them to selecting a NoSQL database:

● Fast-paced Agile development


● Storage of structured and semi-structured data
● Huge volumes of data
● Requirements for scale-out architecture
● Modern application paradigms like microservices and real-time streaming

5
Samundiswary S
Introduction to MongoDB
● MongoDB is a document database designed for ease of development and scaling.
● MongoDB is developed by MongoDB Inc. and categorized as a NoSQL database.
1) Easy to use
● MongoDB is a document-oriented database. It uses the concept of the document to
store data, which is more flexible than the row concept in the relational database
management system (RDBMS).
● A document allows you to represent complex hierarchical relationships with a single
record.
2) Designed to scale out
● When the database grows, you’ll have a challenge of how to scale it. There are two
common ways:
● Scaling up(vertical scaling) – upgrade the current server to a bigger one with more
resources (CPU, RAM, etc). However, getting a bigger server means increasing more
costs.
● Scaling out(Horizontal scaling) – purchase additional servers and add them to the
cluster. This is cheaper and more scalable than scaling up. The downside is that it
takes more effort to manage multiple servers than a big one.

6
Samundiswary S
MongoDB Key Features
● Query Support: Ad-hoc(unscheduled data queries) and document-based
queries are supported by MongoDB.
● Adjustable Schema: You may alter the database’s schema on the fly, which
cuts down on the time it takes to add new features or repair current issues.
● Data Storage: You can store almost any type of data, whether it’s structured,
partially structured, or polymorphic.
● GridFS: A file of any size can be stored without cluttering your stack. GridFS
breaks files into smaller chunks and saves them as individual documents.
● Replication: It has functionality for Master-Slave replication. MongoDB
maintains multiple copies of data using native applications. One of Replica
Set’s benefits is that it has a self-healing shard, which prevents database
downtime.
● Indexing: Without indexing, a database would have to sift through every
document in a collection to find the ones that match the query, which would
be inefficient. As a result, indexing is required for efficient searching, and
MongoDB employs it to analyze large volumes of data quickly.
● MapReduce: For data processing, MongoDB supports MapReduce. Large
amounts of data are consolidated into usable aggregated results using
MapReduce.
7
Samundiswary S
MongoDB Key Features

● MQL: MongoDB employs the MongoDB Query Language (MQL), which is a


powerful and expressive query language. It includes CRUD (Create, Read,
Update, Delete) operations, data aggregation, text search, and geospatial
searches.
● Support for Multiple Servers: MongoDB can run on numerous servers. This
redundancy can protect the system against hardware failure.
● Security: For sensitive data, MongoDB supports security capabilities such as
authentication, access control (user, role-based access control), and
encryption (TLS/SSL). The security features available are also determined by
the pricing tier.
● Sharding: When compared to typical Relational Database choices, MongoDB
provides remarkable scalability. It offers horizontal scaling (sharding), which
allows you to add more Instances with more capacity as needed

8
Samundiswary S
sharding in mongodb

● What is MongoDB Sharding?


● Sharding is a method for distributing or partitioning data across
multiple machines.

● It is useful when no single machine can handle large modern-day


workloads, by allowing you to scale horizontally.

● Horizontal scaling, also known as scale-out, refers to adding


machines to share the data set and load. Horizontal scaling allows for
near-limitless scaling to handle big data and intense workloads .

9
Samundiswary S
Sharding Architecture
● In MongoDB, a sharded cluster consists of:
○ Shards
○ Mongos
○ Config servers
• Shard: Each shard is a replica set that holds a subset of the data. Each
shard is responsible for a portion of the overall dataset.
• Config Servers: Config servers store metadata about the sharded cluster
and manage the distribution of data across shards.
• Query Routers: These servers route client queries to the appropriate shard
based on the data distribution..

10
Samundiswary S
Sharding Benefits

● Sharding allows you to scale your database to handle increased loads to a nearly
unlimited degree. It does this by increasing read/write throughput, and storage capacity.

11
Samundiswary S
Sharding Benefits

12
Samundiswary S
Sharding Benefits

13
Samundiswary S
Data Distribution

14
Samundiswary S
Sharding Strategy

MongoDB supports two sharding strategies for distributing data across sharded clusters:

• Ranged Sharding
• Hashed Sharding
Ranged sharding divides data into ranges based on the shard key values. Each chunk is then
assigned a range based on the shard key values.

15
Samundiswary S
Sharding Strategy

16
Samundiswary S
MongoDB Editions

MongoDB has three editions: community server, enterprise server, and atlas.

1) MongoDB Community Server

The MongoDB Community Edition is free and available on Windows, Linux, and macOS.

MongoDB Community Edition uses the Server Side Public License (SSPL).
2) MongoDB Enterprise Server
MongoDB Enterprise Server is a commercial edition of MongoDB as a part of the
MongoDB Enterprise Advanced subscription.

3) MongoDB Atlas
MongoDB Atlas is a global cloud database service. It is a database as a service that allows
you to focus on building apps rather than spending time managing the databases.

MongoDB Atlas is available on common cloud platforms such as AWS, Azure, and GCP.
MongoDB Atlas has a free tier for your experiments.

17
Samundiswary S
MongoDB Installations
● Download MongoDB Community Server
https://www.mongodb.com/try/download/community

● Install MongoDB Community Server on the local machine


● uncheck the Install MongoDB Compass checkbox and click the Next button. If you
select it, the setup wizard will also install the MongoDB Compass with the community
version.
● MongoDB Compass is a GUI tool that allows you to interact with MongoDB server
including querying, indexing, document validation, and more.
● Download MongoDB Compass
https://www.mongodb.com/try/download/compass
● You’ll find the MongoDB Compass in the Start menu.

● To launch the MongoDB Compass, click the MongoDB Compass icon. The following
shows the main screen of the MongoDB Compass.

● To connect to the local MongoDB Server, click the Connect button:

18
Samundiswary S
MongoDB Installations

19
Samundiswary S
MongoDB Basics
Data formats
● In MongoDB, you will often deal with JSON and BSON formats. Therefore, it’s important to fully
understand them.
 JSON
● JSON stands for JavaScript Object Notation. JSON syntax is based on a subset of JavaScript
ECMA-262 3rd edition.

 BSON
BSON stands for Binary JSON, which is a binary-coded serialization of JSON-like documents.
 Documents
MongoDB stores data records as BSON documents, which are simply called documents.

20
Samundiswary S
MongoDB Basics
A document is a set of field-and-value pairs with the following structure:
{
field_name1: value1,
field_name2: value2,
field_name3: value3,
...
}
Example:
{
_id: ObjectId("5f339953491024badf1138ec"),
title: "MongoDB Tutorial",
isbn: "978-4-7766-7944-8",
published_date: new Date('June 01, 2020'),
author: { first_name: "John"
, last_name: "Doe"}
}
This document has the following field-and-value pairs:
_id holds an ObjectId
title holds a string.
isbn holds a string.
published_date holds a value of the Date type.
author holds an embedded document that contains two fields first_name and last_name.
If you are familiar with a relational database management system (RDBMS), you will find that a
document is similar to a row in a table, but it is much more expressive. 21
Samundiswary S
MongoDB Basics
Collections:
MongoDB stores documents in a collection. A collection is a group of documents.
Unlike a table that has a fixed schema, a collection has a dynamic schema.

It means that a collection may contain documents that have any number of different “shapes”.
For example, you can store the following documents in the same collection:

22
Samundiswary S
MongoDB Basics
Databases:
MongoDB stores collections into a database. A single instance of MongoDB can host multiple
databases.
A database can be referenced by a name for example bookdb.
The database names cannot:
● Be an empty string ("").
● Contain any of these characters: /, \, ., “, *, <, >, :, |, ?, $, (a single space), or \0 (the null
character).
● Exceed the maximum size which is 64 bytes.
● MongoDB also has some reserved database names such as admin, local, and config that
you cannot use to create new databases.

23
Samundiswary S
Introduction to the mongo shell
● Open Mongo Shell
● MongoDB client
● The mongo shell is a MongoDB client. By default, the mongo shell connects to the test
database on the MongoDB server and assigns the database connection to the global
variable called db.
● The db variable allows you to see the current database:
● use the shows dbs command to list all the databases on the server:

• To switch the current database to another, you use the use <database> command.
• Use the show collections to list all collections of the current database.

24
Samundiswary S
Module-4
MongoDB and Building REST API using
MongoDB

IT – TE – Advanced Internet Programming


Lecture -28
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

25
Samundiswary S
Basic CRUD operations
● Create Database using command
use <databasename>
> use books
switched to db books
● To add a document to a collection, you use the insertOne() method of the collection.

• Read
To select documents in a collection, you use the findOne() method:

26
Samundiswary S
Basic CRUD operations
● Using MongoDB insertMany() method to insert multiple documents without specifying _id fields

db.books.insertMany([
{ title: "NoSQL Distilled", isbn: "0-4696-7030-4"},
{ title: "NoSQL in 7 Days", isbn: "0-4086-6859-8"},
{ title: "NoSQL Database", isbn: "0-2504-6932-4"},
]);
Unordered insert example
The following example uses the insertMany() method to perform an unordered insert:

In this example, the _id: 3 is duplicated, MongoDB threw an error.

Since this example used the unordered insert, the operation continued to insert the documents with _id 4 and 5
into the books collection.

27
Samundiswary S
Basic CRUD operations
Update:
● To update a single document, you use the updateOne() method.
● The updateOne() method takes at least two arguments:
● The first argument identifies the document to update.
● The second argument represents the updates that you want to make.

Delete:
To delete a document from a collection, you use the deleteOne() method. The deleteOne() method takes
one argument that identifies the document that you want to delete.
The following example uses the deleteOne() method to delete a document in the books collection:

28
Samundiswary S
Basic CRUD operations
Delete:
To delete a document from a collection, you use the deleteOne() method. The deleteOne() method takes
one argument that identifies the document that you want to delete.
The following example uses the deleteOne() method to delete a document in the books collection:

29
Samundiswary S
pretty() Method-To format the output

30
Samundiswary S
MongoDB Projection
● Projection simply means selecting fields to return from a query.

● By default, the find() and findOne() methods return all fields in matching documents.
Most of the time you don’t need data from all the fields.
● To select fields to return from a query, you can specify them in a document and pass
the document to the find() and findOne() methods. This document is called a projection
document.

31
Samundiswary S
MongoDB Projection
● MongoDB projection examples Returning all fields except for some fields
If the number of fields to return is many, you can use the
db.products.find({price: 899}); projection document to exclude other fields instead.

32
Samundiswary S
MongoDB Data Types
The null type is used to represent a null and a field that does not exist.
For example:{
"isbn": null
}
Boolean
The boolean type has two values true and false. For example:

{
"best_seller": true
}
Number
By default, the mongo shell uses the 64-bit floating-point numbers. For example:

{
"price": 9.95,
"pages": 851
}
The NumberInt and NumberLong classes represent 4-byte and 8-byte integers respectively. For
example:

{
"year": NumberInt("2020"),
"words": NumberLong("95403")
}

33
Samundiswary S
MongoDB Data Types
The date type stores dates as 64-bit integers that represents milliseconds since the Unix epoch
(January 1, 1970). It does not store the time zone. For example:
{
"updated_at": new Date()
}
Regular Expression
MongoDB allows you to store JavaScript regular expressions. For example:

{
"pattern": /\d+/
}
Array
The array type allows you to store a list of values of any type. The values do not have to be in
the same type, for example:
{
"title": "MongoDB Array",
"reviews": ["John", 3.5, "Jane", 5]
}
Embeded Document
A value of a document can be another document that is often referred to as an embedded
document.

34
Samundiswary S
Module-4
MongoDB and Building REST API using
MongoDB

IT – TE – Advanced Internet Programming


Lecture -29
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

35
Samundiswary S
Administering User Accounts
Built-in Roles
Provides access to data and actions to MongoDB through role-based
authorization and has built-in roles that provide several levels of access in the
database.

● User Database: Roles Database users have a role to manipulate data in non-
system collection.
● Examples : read, readWrite.
● Database Administration: Roles Database Administration deals with
administrative management of databases such as user administration,
schema, and objects in it.
● Examples : dbAdmin, userAdmin, dbOwner.
● Cluster Administration: The role of cluster administration is to administer
the entire MongoDB system, including its replica sets and shards.
● Examples : clusterAdmin, clusterManager

36
Samundiswary S
Administering User Accounts
● Backup and Restoration: This Roles is specific for functions
related to database backup in MongoDB.
○ Examples : backup, restore.
● All-Database Roles: Roles are in the database admin and have
access to all databases except local and config.
○ Examples : readAnyDatabase, readWriteAnyDatabase,
userAdminAnyDatabase.
● Superuser: Roles has the ability to grant access to every user, to
every privilege, in all databases.
○ Example: root

https://www.mongodb.com/docs/manual/reference/built-in-roles/

37
Samundiswary S
User Defined Roles

● Roles are also created according to needs, what privileges we will


give to those roles.
● To create roles, you can use the db.createRole() function command.
● Besides being able to create roles, there are several other functions
to manage existing roles such as: db.dropRole() which is useful for
deleting existing roles in the database, db.getRole() functions to get
all information from specific roles.

38
Samundiswary S
Commands and methods

● To create, modify, and delete users within MongoDB and configure


authentication, the core methods you need are:
● db.createUser: create a new MongoDB user account
● db.updateUser: update the details of a user account
● db.changeUserPassword: change the password used by a user
account
● db.dropUser: delete a MongoDB user account
● Additionally, the following database command is useful for finding
information about users on the system:
● db.runCommand('usersInfo'): show information about one or more
MongoDB user accounts

39
Samundiswary S
Understanding how MongoDB implements users and
authentication
● In MongoDB, user accounts are a combination of the account
username along with a specific authentication database.
● The authentication database is simply the database where the user is
defined and does not imply a limitation on scope or rights.
Authentication databases are regular databases used to manage
other data and are not special, dedicated databases.
● A user account name must be unique in its authentication database.
However, the same username may be reused with a different
authentication database to create a new, distinct user account.
● To authenticate to an account, one also needs to be able to provide
the credentials associated with an account. This is usually a
password, but can also be a certificate.

40
Samundiswary S
MongoDB-Authentication

● Authentication is the process of verifying the identity of a client. When access control
(authorization) is enabled, MongoDB requires all clients to authenticate themselves in
order to determine their access.

● Although authentication and authorization are closely connected, authentication is


distinct from authorization:

● Authentication verifies the identity of a user.


● Authorization determines the verified user's access to resources and operations.
Authentication Mechanisms

● SCRAM Authentication

● x.509 Certificate Authentication


● Kerberos Authentication
● LDAP Proxy Authentication

41
Samundiswary S
Managing MongoDB User & Roles

● To create a new user, you must first switch to the database you want
to use as the new user's authentication database.
● First, you can get a list of the databases that are already configured
on your system by typing:
1. show dbs
2. Switch to the database the user will be associated with using the use
command: use admin

42
Samundiswary S
Module-4
MongoDB and Building REST API using
MongoDB

IT – TE – Advanced Internet Programming


Lecture -30
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

43
Samundiswary S
Administering User Accounts-Example

44
Samundiswary S
To create a new Admin user

● start mongodb in cmd prompt

● run mongo shell

● Create admin User:

45
Samundiswary S
To create a new user

46
Samundiswary S
How do you show existing users?
● To return multiple users,
you can use the
db.getUsers() method on
to show all of the users
within the current
database.
• To additionally show each
user's credential
information, pass an
object to the method with
the showCredentials key
to true:

db.getUsers({
showCredentials: true
})
47
Samundiswary S
change the password for a MongoDB user
● To change a user's password, you can use the
db.changeUserPassword()method. (*switch to the user's authentication
database before executing the command.)
● The method takes two arguments:
● the username of the account you wish to change and
● the new password for the account.

48
Samundiswary S
delete MongoDB users

db.dropUser("student3”)

49
Samundiswary S
MongoDB –Authenticating user

● Step 1: Run server


● Step2:Open another cmd prompt and login through admin user

Admin username
and pwd

User “sam” don’t have admin


rights .only readWrite rights to
database reports.so
authentication failed in admin DB
but authentication success in DB
“reports

50
Samundiswary S
List users
● use admin
● switched to db admin
> db.system.users.find()

51
Samundiswary S
Administering User Accounts

Authorization in MongoDB (Role-Based Access Control)


● In MongoDB, you control who has access to what resources on a database
and to which degree through a mechanism called Role-Based Access Control,
often shortened as RBAC.
● In Role-Based Access Control, users are not given permissions to perform
actions on resources directly, such as inserting a new document into the
database or querying a particular collection. This would make the security
policies difficult to manage and keep consistent with many users in the
system. Instead, the rules allowing actions on particular resources are
assigned to roles.
● Roles are defined with a set of one or more privileges. Each privilege consists
of an action (such as creating new documents, retrieving data from a
document, or creating and deleting users) and the resource on which that
action can be performed (such as a database named reports or a collection
called orders).

52
Samundiswary S
User authentication in mongoDB Compass

53
Samundiswary S
Module-4
MongoDB and Building REST API using
MongoDB

IT – TE – Advanced Internet Programming


Lecture -31
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

54
Samundiswary S
Introduction to Node.js
● Node.js is an open source server environment.
● Node.js allows you to run JavaScript on the server
● Node.js is a server-side platform built on Google Chrome's JavaScript Engine
(V8 Engine). Node.js was developed by Ryan Dahl in 2009 and its latest
version is v0.10.36.
● Node.js applications are written in JavaScript, and can be run within the
Node.js runtime on OS X, Microsoft Windows, and Linux.
● Node.js also provides a rich library of various JavaScript modules which
simplifies the development of web applications using Node.js to a great
extent.
Node.js = Runtime Environment + JavaScript Library

55
Samundiswary S
Why Node.js
● Node.js uses asynchronous programming!
● A common task for a web server can be to open a file on the server and return
the content to the client.
Here is how PHP or ASP handles a file request:
● Sends the task to the computer's file system.
● Waits while the file system opens and reads the file.
● Returns the content to the client.
● Ready to handle the next request.
Here is how Node.js handles a file request:
● Sends the task to the computer's file system.
● Ready to handle the next request.
● When the file system has opened and read the file, the server returns the
content to the client.
● Node.js eliminates the waiting, and simply continues with the next request.
● Node.js runs single-threaded, non-blocking, asynchronous programming, which
is very memory efficient.
What Can Node.js Do?
● Node.js can generate dynamic page content
● Node.js can create, open, read, write, delete, and close files on the server
● Node.js can collect form data
● Node.js can add, delete, modify data in your database
56
Samundiswary S
Node.js MySQL

● Node.js can be used in database applications.


● One of the most popular databases is MySQL.
Install MySQL Driver
● Once you have MySQL up and running on your computer, you can access it
by using Node.js.
● To access a MySQL database with Node.js, you need a MySQL driver. This
tutorial will use the "mysql" module, downloaded from NPM.
● To download and install the "mysql" module, open the Command Terminal and
execute the following:
C:\Users\Your Name>npm install mysql

57
Samundiswary S
Install MongoDB Driver

● To download and install the official MongoDB driver, open the Command
Terminal and execute the following:

● Download and install mongodb package:

C:\Users\Your Name>npm install mongodb


Node.js can use this module to manipulate MongoDB databases:

var mongo = require('mongodb');

58
Samundiswary S
Adding the MongoDB Driver to Node.js
Create a file nodeconnection.js

59
Samundiswary S
Creating database

● To create a database in MongoDB, start by creating a MongoClient object, then specify


a connection URL with the correct ip address and the name of the database you want to
create.
● MongoDB will create the database if it does not exist, and make a connection to it.

60
Samundiswary S
Creating collections
● To create a collection in MongoDB, use the createCollection() method:
● Create a nodejs file collection .js as shown below

61
Samundiswary S
Creating collections

● Run below command to execute node js file

Run mongo shell and check


created data base and collection
is available

62
Samundiswary S
Insert Into Collection
● To insert a record, or document as it is called in MongoDB, into a collection,
we use the insertOne() method.
● A document in MongoDB is the same as a record in MySQL
● Insert a document In created collection(node_collection1)

63
Samundiswary S
Node.js MongoDB Find

64
Samundiswary S
Node.js MongoDB Query

65
Samundiswary S
Module-4
MongoDB and Building REST API using
MongoDB

IT – TE – Advanced Internet Programming


Lecture -32
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

66
Samundiswary S
Using Mongoose
● Node.js Mongoose is a MongoDB object modeling tool designed to work in an
asynchronous environment.
● Mongoose is an Object Document Mapper (ODM). This means that
Mongoose allows you to define objects with a strongly-typed schema that is
mapped to a MongoDB document.
● With the help of Mongoose, we can model our data.
● Mongoose provides a straight-forward, schema-based solution to modeling
your application data and includes built-in type casting, validation, query
building, business logic hooks and more, out of the box.

67
Samundiswary S
Advantages of using Mongoose module
● Mongoose module is one of the most powerful external modules of NodeJS.
Mongoose is a MongoDB ODM i.e (Object database Modelling) that used to
translate the code and its representation from MongoDB to the Node.js
server.

Advantages of Mongoose module:

● Collection validation of the MongoDB database can be done easily.


● Predefined Structure can be implemented on the collection.
● Constraints can be applied to documents of collections using Mongoose.
● Mongoose module built on the top of MongoDB driver and provides easily
abstraction of the query and define a query.
● Many developers that are habitual of using SQL feel uncomfortable while
working on the MongoDB because Nosql databases and flexible structure
here Mongoose plays an the important role and makes the collection schema
similar to the SQL databases.
● Mongoose module provides several functions in order to manipulate the
documents of the collection of the MongoDB database.

68
Samundiswary S
Mongoose

Object Data Modeling in MongoDB


● A huge benefit of using a NoSQL database like MongoDB is that you are not
constrained to a rigid data model. You can add or remove fields, nest data
multiple layers deep, and have a truly flexible data model that meets your
needs today and can adapt to your ever-changing needs tomorrow.
● But being too flexible can also be a challenge. If there is no consensus on
what the data model should look like, and every document in a collection
contains vastly different fields, you're going to have a bad time.
● With Mongoose, you would define a Schema object in your application code
that maps to a collection in your MongoDB database.
● The Schema object defines the structure of the documents in your collection.
Then, you need to create a Model object out of the schema. The model is
used to interact with the collection.

69
Samundiswary S
Mongoose Installation
● To install Mongoose package, use npm (Node Package Manager). Open a
terminal and run the following command.

npm install mongoose

Using Mongoose
To use Mongoose in Node.js Script File, include
require statement with mongoose package.

var mongoose = require('mongoose');

70
Samundiswary S
Node.js Mongoose – Connect to MongoDB

● To connect to MongoDB from Node.js using Mongoose package, call connect() function, on the
variable referencing to mongoose, with MongoDB Database URI passed as argument to the
function.
var mongoose = require('mongoose’);
mongoose.connect('mongodb://localhost:27017/database_name');
● Get reference to Database
To get a reference to the database specified, use connection() function on Mongoose reference, as
shown below :

var db = mongoose.connection;

71
Samundiswary S
Mongoose Model
● A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema
defines the structure of the document, default values, validators, etc., whereas a
Mongoose model provides an interface to the database for creating, querying, updating,
deleting records, etc.
● Creating a Mongoose model comprises primarily of three parts:
1. Referencing Mongoose
let mongoose = require('mongoose’)
2. Defining the Schema
A schema defines document properties through an object where the key name corresponds
to the property name in the collection.
let emailSchema = new mongoose.Schema({
email: String
})
The following Schema Types are permitted:
Array
Boolean
Buffer
Date
Mixed (A generic / flexible data type)
Number
ObjectId
String

72
Samundiswary S
Mongoose Model

3. Exporting a Model
● We need to call the model constructor on the Mongoose instance and pass it the name
of the collection and a reference to the schema definition.
module.exports = mongoose.model('Email', emailSchema)

73
Samundiswary S
Node.js Mongoose – Define a Model

74
Samundiswary S
Executing Operations on MongoDB with Mongoose

75
Samundiswary S
Mongoose connect using async/await

● The connect() method provided by the Mongoose supports both JavaScript promises and async-
await syntax.

76
Samundiswary S
Mongoose connect using async/await

● To use async-await syntax, you need to write an asynchronous function, as shown below

77
Samundiswary S
Validation
● Before we get into the specifics of validation syntax, please keep the following
rules in mind:
● Validation is defined in the SchemaType
● Validation is middleware. Mongoose registers validation as a pre('save') hook
on every schema by default.
● You can disable automatic validation before save by setting the
validateBeforeSave option
● You can manually run validation using doc.validate(callback) or
doc.validateSync()
● You can manually mark a field as invalid (causing validation to fail) by using
doc.invalidate(...)
● Validators are not run on undefined values. The only exception is the required
validator.
● Validation is asynchronously recursive; when you call Model#save, sub-
document validation is executed as well. If an error occurs, your Model#save
callback receives it
● Validation is customizable

78
Samundiswary S
Built-in Validators

● Mongoose has several built-in validators.

● All SchemaTypes have the built-in required validator. The required validator uses the
SchemaType's checkRequired() function to determine if the value satisfies the
required validator.
● Numbers have min and max validators.
● Strings have enum, match, minLength, and maxLength validators.

79
Samundiswary S
Mongoose Validation Examples-required

80
Samundiswary S
minlength and maxlength

81
Samundiswary S
Module-4
MongoDB and Building REST API using
MongoDB

IT – TE – Advanced Internet Programming


Lecture -33
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

82
Samundiswary S
API

 What is an API?
● APIs are mechanisms that enable two software components to communicate
with each other using a set of definitions and protocols. For example, the
weather bureau’s software system contains daily weather data. The weather
app on your phone “talks” to this system via APIs and shows you daily
weather updates on your phone.
 What does API stand for?
● API stands for Application Programming Interface. In the context of APIs, the
word Application refers to any software with a distinct function. Interface can
be thought of as a contract of service between two applications. This contract
defines how the two communicate with each other using requests and
responses. Their API documentation contains information on how developers
are to structure those requests and responses.

83
Samundiswary S
How do APIs work?
 API architecture is usually explained in terms of client and server. The application sending
the request is called the client, and the application sending the response is called the
server. So in the weather example, the bureau’s weather database is the server, and the
mobile app is the client.
 There are four different ways that APIs can work depending on when and why they were
created.
SOAP APIs
 These APIs use Simple Object Access Protocol. Client and server exchange messages
using XML. This is a less flexible API that was more popular in the past.
RPC APIs
 These APIs are called Remote Procedure Calls. The client completes a function (or
procedure) on the server, and the server sends the output back to the client.
Websocket APIs
 Websocket API is another modern web API development that uses JSON objects to pass
data. A WebSocket API supports two-way communication between client apps and the
server. The server can send callback messages to connected clients, making it more
efficient than REST API.
REST APIs
 These are the most popular and flexible APIs found on the web today. The client sends
requests to the server as data. The server uses this client input to start internal functions
and returns output data back to the client.
 Let’s look at REST APIs in more detail below.

84
Samundiswary S
REST API

● What are REST APIs?


● REST stands for Representational State Transfer. REST defines a set of functions
like GET, PUT, DELETE, etc. that clients can use to access server data. Clients and
servers exchange data using HTTP.
● The main feature of REST API is statelessness. Statelessness means that servers
do not save client data between requests. Client requests to the server are similar to
URLs you type in your browser to visit a website. The response from the server is
plain data, without the typical graphical rendering of a web page.
● What is web API?
● A Web API or Web Service API is an application processing interface between a web
server and web browser. All web services are APIs but not all APIs are web services.
REST API is a special type of Web API that uses the standard architectural style
explained above.
● The different terms around APIs, like Java API or service APIs, exist because
historically, APIs were created before the world wide web. Modern web APIs are
REST APIs and the terms can be used interchangeably.

85
Samundiswary S
REST API
What are API integrations?
API integrations are software components that automatically update data between clients and
servers. Some examples of API integrations are when automatic data sync to the cloud from your
phone image gallery, or the time and date automatically sync on your laptop when you travel to
another time zone. Enterprises can also use them to efficiently automate many system functions.
What are the benefits of REST APIs?
REST APIs offer four main benefits:
1. Integration
APIs are used to integrate new applications with existing software systems. This increases
development speed because each functionality doesn’t have to be written from scratch. You
can use APIs to leverage existing code.
2. Innovation
Entire industries can change with the arrival of a new app. Businesses need to respond quickly
and support the rapid deployment of innovative services. They can do this by making changes
at the API level without having to re-write the whole code.
3. Expansion
APIs present a unique opportunity for businesses to meet their clients’ needs across different
platforms. For example, maps API allows map information integration via websites,
Android,iOS, etc. Any business can give similar access to their internal databases by using
free or paid APIs.
4. Ease of maintenance
The API acts as a gateway between two systems. Each system is obliged to make internal
changes so that the API is not impacted. This way, any future code changes by one party do
not impact the other party. 86
Samundiswary S
REST API

● What is an API endpoint and why is it important?


● API endpoints are the final touchpoints in the API communication system. These
include server URLs, services, and other specific digital locations from where
information is sent and received between systems. API endpoints are critical to
enterprises for two main reasons:

1. Security
● API endpoints make the system vulnerable to attack. API monitoring is crucial for
preventing misuse.

2. Performance
● API endpoints, especially high traffic ones, can cause bottlenecks and affect system
performance.

87
Samundiswary S
The rules of a REST API
● In basic terms, a REST API takes an incoming HTTP request, does some processing,and always
sends back an HTTP response

88
Samundiswary S
Request URLs
● The common actions you’ll likely want are
 Create a new item
 Read a list of several items
 Read a specific item
 Update a specific item
 Delete a specific item
the database has a Locations collection that you want to interact with. Table
6.1 shows how the URL paths might look for this collection. Note that all
URLs have the same base path and, where used, have the same location ID
parameter.

89
Samundiswary S
Request methods
● HTTP requests can have different methods that essentially tell the server what type of action to take.
The most common type of request is a GET request—the method used when you enter a URL in the
address bar of your browser. Another common method is POST, often used for submitting form data.

Request methods that link URLs to the desired actions, enabling the API to use the same URL for
different actions

90
Samundiswary S
Commonly used HTTP methods

91
Samundiswary S
Responses and status codes
● For a successful REST API, standardizing the responses is as important as
standardizing the request format. There are two key components to a response:
●  The returned data
●  The HTTP status code
● Combining the returned data with the appropriate status code should give the
requester all the information required to continue.
RETURNING DATA FROM AN API
● Your API should return a consistent data format. Typical formats for a REST API
are XML and/or JSON. You’ll use JSON for your API, because it’s the natural fit for
the MEAN stack. MongoDB outputs JSON, which Node and Angular can both
natively understand.
● JSON is, after all, the JavaScript way of transporting data. JSON is also more
compact than XML, so it can help speed the response times and efficiency of an
API by reducing the bandwidth required.
● Your API will return one of three things for each request:
 A JSON object containing data answering the request query
 A JSON object containing error data
 A null response

92
Samundiswary S
Most popular HTTP status codes and how they
might be used to send responses to an API Request

93
Samundiswary S
Module-4
MongoDB and Building REST API using
MongoDB

IT – TE – Advanced Internet Programming


Lecture -34
Samundiswary S
Designation
Dept. of Information Technology,
SIES Graduate School of Technology

94
Samundiswary S
What is Express?

● Express provides a minimal interface to build our applications. It provides us


the tools that are required to build our app. It is flexible as there are numerous
modules available on npm, which can be directly plugged into Express.
● web application framework that provides broad features for building web and
mobile applications. It is used to build a single page, multipage, and hybrid
web application.

● It's a layer built on the top of the Node js that helps manage servers and
routes

95
Samundiswary S
96
Samundiswary S
Setting up the API in Express

● Install Express by
npm install express

● Install MongoDB and mongoose if not having in machine


npm install mongodb
npm install mongoose
● if any change is made in server.to reflect automatically without restarting the server
npm install -g nodemon --save-dev

97
Samundiswary S
ExpressJS - RESTFul APIs
● We will be creating a movies API towards the end; let us now discuss how it
will be structured.

98
Samundiswary S
ExpressJS - RESTFul APIs
● Let us now create this API in Express. We will be using JSON as our transport
data format as it is easy to work with in JavaScript and has other benefits.
Replace your index.js file with the movies.js file as in the following program.

● index.js

99
Samundiswary S
ExpressJS - RESTFul APIs
● Once you import Express then, create a Router and export it using
module.exports −

100
Samundiswary S
ExpressJS - RESTFul APIs

101
Samundiswary S
ExpressJS - RESTFul APIs

102
Samundiswary S
ExpressJS - RESTFul APIs

103
Samundiswary S
Testing end points

https://apidog.com/blog/how-to-use-postman-for-api-testing/

104
Samundiswary S
References

● https://www.mongodbtutorial.org/
● https://www.digitalocean.com/community/tutorials/how-to-use-mongodb-acces
s-control#how-mongodb-controls-access-with-role-based-access-control
● https://studio3t.com/knowledge-base/articles/mongodb-user-authentication/
● https://masteringjs.io/mongoose
● https://nodejs.dev/learn/javascript-asynchronous-programming-and-callbacks
● https://javascript.info/async-await
● https://mongoosejs.com/docs/queries.html
● https://vegibit.com/mongoose-validation-examples/
● https://attacomsian.com/blog/mongoose-connect-async-await

105
Samundiswary S

You might also like