III
III
III
UNIT 3
NoSQL stands for Not only SQL. NoSQL Databases are also known as non-relational
databases that don’t require a fixed schema.
Features of NoSQL
➢ NoSQL information bases are largely utilized for BigData and ongoing applications.
➢ NoSQL database processes information in a distributed manner and can oblige
tremendous volumes of data.
➢ is used to refer to a non-SQL or nonrelational database.
➢ It provides a mechanism for the storage and retrieval of data other than the tabular
relations model used in relational databases.
➢ NoSQL database doesn't use tables to store data. It is generally used to store big data and
real-time web applications.
MongoDB
MongoDB is a document-oriented NoSQL database system that provides high scalability,
flexibility, and performance. Unlike standard relational databases, MongoDB stores data in
a JSON document structure form. This makes it easy to operate with dynamic and unstructured
data and MongoDB is an open-source and cross-platform database System.
MongoDB data types and corresponding ID number
Type Number
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular Expression 11
JavaScript 13
JavaScript (with scope) 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Decimal126 19
Min key -1
Max key 127
Step 1: Go to the MongoDB Download Center to download the MongoDB Community Server.
Step 2: When the download is complete open the msi file and click the next button in the startup
screen
Step 3: Accept the End-User License Agreement and click the next button.
Step 4: Now select the complete option to install all the program features. Here, if you can want
to install only selected program features and want to select the location of the installation, then
use the Custom option.
Step 5: Select “Run service as Network Service user” and copy the path of the data
directory. Click Next
Step 6: Click the Install button to start the MongoDB installation process.
Step 7: After clicking on the install button installation of MongoDB begins.
Step 8: Now click the Finish button to complete the MongoDB installation process:
Step 9: Now we go to the location where MongoDB installed in step 5 in your system and copy
the bin path
Step 10: Now, to create an environment variable open system properties >> Environment
Variable >> System variable >> path >> Edit Environment variable and paste the copied link
to your environment system and click Ok.
Step 11: After setting the environment variable, we will run the MongoDB server,
i.e. mongod. So, open the command prompt and run the following command
Step 12: Now, Open C drive and create a folder named “data” Inside this folder create another
folder named “db”. After creating these folders. Again open the command prompt and run the
following command: mongod
Now, this time the MongoDB server(i.e., mongod) will run successfully
Starting MongoDB
Once you have installed MongoDB, you need to be able to start and stop thedatabase
engine. The database engine starts by executing the mongod (mongod.exeon Windows)
executable in the
<mongo_install_location>/binlocation. This executable starts MongoDBand begins listening for
database requests on the configured port.
The mongod executable accepts several different parameters that provide methods of
controlling its behavior. For example, you can configure the IP address and port MongoDB
listens on as well as logging and authentication. Table 12.1 provides a list of some of the most
commonly used parameters.
-f <filename>
--port <port> Specifies a TCP port for mongodto listen for client
connections. Default: 27017.
--bind_ip <ipaddress> <path>
--maxConns
<number>
--logpath
Specifies the IP address Specifies the maximum number of simultaneous connections that mongodwill
on which mongod will accept. Max: 20000
bind to and listen for
connections. Default: Specifies a path for the log file. On restart, the log file isoverwritten unless you
All Interfaces also specify --logappend.
To stop the MongoDB database from the shell client, use the following commands toswitch to the admin database
and then shut down the database engine:
Useadmin
db.shutdownServer()
Once you have installed, configured, and started MongoDB, you can access it through the MongoDB
shell. The MongoDB shell is an interactive shell provided with MongoDB that allows you to access, configure,
and administer MongoDB databases, users, and much more. You use the shell for everything from setting up
user accounts to creating databases to querying the contents of the database.
The following sections take you through some of the most common administration tasks that you perform in
the MongoDB shell. Specifically, you need to be able to create user accounts, databases, and collections to
follow the examples in the rest ofthe book. Also you should be able to perform at least rudimentary queries
on documents to help you troubleshoot any problems with accessing data.
To start the MongoDB shell, first make sure that mongod is running, and then run the mongodcommand, then
execute the mongocommand from the console prompt.The shell should start up as shown in Figure 12.1.
Once you have accessed the MongoDB shell, you can administer all aspects of MongoDB. There are a couple
of things to keep in mind when using MongoDB. First is that it is based on JavaScript and most of its syntax is
available. Second, theshell provides direct access to the database and collections on the server so changesyou
make directly impact the data on the server.
The MongoDB shell provides several commands that can be executed from the shellprompt. You need to be
familiar with these commands as you will use them a lot.
The following list describes each command and its purpose:
• help <option>: Displays syntax help for MongoDB shell commands. The option argument allows you to
specify an area where you want help.
• use <database>: Changes the current database handle. Database operations are processed using the
current database handle.
• db. help: Displays help options for the database methods.
• show <option>: Shows a list based on the optionargument. The value of an optioncan be:
• dbs: Displays a list of databases.
• collections: Displays a list of collections for the current database.
• profile: Displays the five most recent systems. profileentries taking more than 1 millisecond.
• databases: Displays a list of all available databases.
• roles: Displays a list of all roles for the current database, both built-in anduser-defined.
• users: Displays a list of all users for that database.
• exit: Exits the database.
The MongoDB shell also provides a number of methods to perform administrative functions. These methods
can be called directly from the MongoDB shell or from ascript executed in the MongoDB shell.
There are many methods that you can use to perform various administrative functions. Some of these are
covered in later sections and chapters in this book. Fornow, you need to be aware of the types of shell methods
and how to access them.
The following list provides a few examples of shell methods:
• load(script): This function loads and runs a JavaScript file inside the shell. It is a great way to script
database operations.
• UUID(string): Converts a 32-byte hex string into a BSON UUID.
• db.auth(username, password): Authenticates you to the currentdatabase.
Creating users and assigning roles in MongoDB is essential for managing access control and securing
your databases. MongoDB provides flexibility in defining user privileges through roles, allowing users
to perform specific operations on designated resources.
How to Create user and add a role in MongoDB???
In MongoDB, users are created using createUser() method.
user: It contains authentication and access information about the user to create. It is a
document.
• user: Name of the user
• pwd: User passw ord. This field is not required if you use this method on $external
database to create a user whose credentials are stored externally. The value of this field
can be of string type or passwordPrompt().
• customData: User Associative Information. It is an optional field.
• roles: Access Level or Privilege of a user. You can also create a user without roles by
passing an empty array[].
writeConcern: It is an optional parameter. It manages the level of Write Concern for the creation
operation.
Create An Administrative User
We can create the name, password, and administrative user roles.
db.createUser(
{
user: "hello_admin",
pwd: "hello123",
roles:
[
{ role:"readWrite",db:"config"},
"clusterAdmin"
] } );
In MongoDB, we can create a user without any roles by specifying an empty array[] in the role field in
the createUser() method.
Syntax:
db.createUser({ user:”User_Name”, pwd:”Your_Password”, roles:[]});
In MongoDB, we can create a user with some specified roles using the createUser() method. In this
method, we can specify the roles that the user will do after creating.
Let us discuss this concept with the help of an example:
Example:
In this example, we are going to create a user with some specified roles.
db.createUser(
{
user: "restrict",
pwd: passwordPrompt(),
roles: [ { role: "readWrite", db: "example" } ],
authenticationRestrictions: [ {
clientSource: ["192.168.65.10"],
serverAddress: ["198.157.56.0"]
}]
}
)
show dbs
If you want to get data in a structured form, then use pretty() method with find() method.
db.collection.find().pretty()
2. insertMany() method
insertMany() method is used to insert many documents in the collection
Open the mongod.conf file in any editor and write the following under security,
Save the changes and close the file. Once we have made the changes, Go to Services in Windows and
find MongoDB and restart it.
db.createUser({
... "user": "Geek",
... "pwd": "abc123",
... "roles": [ { "role": "userAdmin", "db": "mydb" } ]
... })
we need to authenticate first to get the data. That means we have successfully enabled authentication
and access control.
let’s first give the username and password.
db.auth("Geek","abc123")
db.nameCollection.find()
Method Description
Method Description
3. Update Operations
The update operations are used to update or modify the existing document in the collection. You can perform
update operations using the following methods provided by the MongoDB.
Method Description
4. Delete Operations
The delete operation are used to delete or remove the documents from a collection. You can perform delete
operations using the following methods provided by the MongoDB.
Method Description
const contactSchema = {
email: String,
query: String,
};
Step 5: Create a Model with the defined schema
const Contact = mongoose.model("Contact", contactSchema);
app.post("/contact", function (req, res) {
const contact = new Contact({
email: req.body.email,
query: req.body.query,
});
contact.save(function (err) {
if (err) {
res.redirect("/error");
} else {
res.redirect("/thank-you");
}
});
});
const express = require("express"); {
const ejs = require("ejs"); res.render("contact");
const mongoose = require("mongoose"); });
const bodyParser = require("body-parser"); app.post("/contact",
mongoose.connect( function (req, res) {
"mongodb://localhost:27017/newCollection", console.log(req.body.email);
{ const contact = new Contact({
useNewUrlParser: true, email: req.body.email,
useUnifiedTopology: true query: req.body.query,
}); });
const contactSchema = { contact.save(function (err) {
email: String, if (err) {
query: String, throw err;
}; } else {
const Contact = res.render("contact");
mongoose.model("Contact", contactSchema); }
const app = express(); });
app.set("view engine", "ejs"); });
app.use(bodyParser.urlencoded({ app.listen(3000,
extended: true function () {
})); console.log("App is running on Port 3000");
app.use(express.static(__dirname + '/public')); });
app.get("/contact",
function (req, res)
IV Administering databases.
Database administration in MongoDB involves managing the MongoDB environment to ensure
optimal performance, security, and reliability. Key aspects of administering a MongoDB database
include:
1. User Management and Access Control
• User Creation: Admins can create users with specific roles and permissions using commands
like db.createUser().
• Role-Based Access Control (RBAC): MongoDB employs RBAC, allowing administrators to
define roles that dictate what actions users can perform on the database (e.g., read, write,
dbAdmin).
2. Database Backup and Restore
• Backup: Administrators can back up data using tools like mongodump for logical backups or file
system snapshots for physical backups.
• Restore: The mongorestore command is used to restore data from backups, ensuring data
integrity during recovery.
3. Performance Monitoring and Tuning
• Monitoring Tools: MongoDB provides tools like the MongoDB Atlas performance monitoring,
as well as the mongostat and mongotop commands for real-time statistics.
• Indexing: Effective indexing strategies can significantly enhance query performance. Admins
must regularly review and optimize indexes based on query patterns.
4. Replication and High Availability
• Replica Sets: Admins can configure replica sets to ensure high availability and data redundancy.
A replica set consists of a primary node and one or more secondary nodes.
• Automatic Failover: In the event of a primary node failure, MongoDB automatically promotes a
secondary node to primary, ensuring continuous data availability.
5. Sharding for Scalability.
• Sharding: MongoDB supports sharding, which distributes data across multiple servers to handle
large datasets and high throughput. Admins define shard keys to efficiently partition data.
6. Data Management and Maintenance
• Data Modeling: Effective schema design is crucial for performance. Admins should consider
data relationships, access patterns, and document size when modeling data.
• Data Migration: MongoDB provides tools and commands (like mongoimport and mongoexport)
for importing and exporting data between different MongoDB instances.
V Managing collections Connectivity.
Managing Collections
As a database admin, you may also find yourself administering collections within a database. MongoDB
provides the functionality in the MongoDB shell to create, view, and manipulate collections in a database.
The following sections cover the basics that you need to know to use the MongoDB shell to list collections,
create new ones, and access the documents contained within them.
Often you may need to just see a list of collections contained in a database—for example, to verify that a
collection exists or to find the name of a collection that you cannot remember. To see a list of collections in a
database, you need to switch to that database and then use show collections to get the list of collections
containedin the database. For example, the following commands list the collections in the test database:
use test
show collections
Creating Collections
You must create a collection in MongoDB database before you can begin storing documents. To create a
collection, you need to call createCollection(name,[options])on the database handle. The nameparameter is the
name of the new database. The optional optionsparameter is an object that can have the propertieslisted in Table
12.4 that define the behavior of the collection.
Table 12.4 Options that can be specified when creating collectionsRole Description
A Boolean; when true, the collection is a capped collection that does not grow bigger than the maximum
size specified by the sizeattribute. Default: false.
A Boolean; when true, an _id field is automatically created for each document addedto the collection and an
index on that field is implemented. This should be falseforcapped collections. Default: true.
Specifies the size in bytes for the capped collection. The oldest document is removed tomake room for new
documents.
Specifies the maximum number of documents allowed in a capped collection. The oldest document is
removed to make room for new documents.
Allows users to specify validation rules orexpressions for the collection.
validationLevel Determines the strictness MongoDB applies to
For example, the following lines of code create a new collection callednewCollectionin the testDBdatabase as
shown in Figure 12.7:
db.createCollection("newCollection")
Deleting Collections
Occasionally you also want to remove old collections when they are no longer needed. Removing old
collections frees up disk space and eliminates any overheadsuch as indexing associated with the collection.
To delete a collection in the MongoDB shell, you need to switch to the correct database, get the collection object,
and then call the drop() function on that object. For example, the following code deletes the newCollection
collection from the testDBdatabase as shown in Figure 12.8:
use testDB
show collections
coll = db.getCollection("newCollection")coll.drop()
show collections
Figure 12.8 Deleting a collection in the MongoDB console shell
Most of the time you use a library such as the native MongoDB driver or Mongooseto access documents in a
collection. However, sometimes you might need to look atdocuments inside the MongoDB shell.
The MongoDB shell provides full querying functionality to find documents in collections using the find(query)
method on the collection object. The optionalquery parameter specifies a query document with fields and
values to match documents against in the collection. The documents that match the query are removed from
the collection. Using the find() method with no query parameter returns all documents in the collection.
For example, the following lines of code first query every item in the collection and then retrieve the
documents whose speedfield is equal to 120mph. The results areshown in Figure 12.9.
use testDB
coll = db.getCollection("newCollection")coll.find()
coll.find({speed:"120mph"})
Typically, the insertion of documents in a collection should be done through your Node.js application.
However, at times you may need to manually insert a document from an administrative point of view to
preload a database, to fix a database, or for testing purposes.
To add documents to a collection, you need to get the collection object and then callthe insert(document) or
save(document) method on that object. The document parameter is a well-formatted JavaScript object that is
converted to BSON and stored in the collection. As an example, the following commands create three new
objects inside a collection as shown in Figure 12.10:
use testDB
coll = db.getCollection("newCollection")coll.find()
coll.insert({ vehicle: "plane", speed: "480mph" })coll.insert({ vehicle: "car", speed:
"120mph" }) coll.insert({ vehicle: "train", speed: "120mph" })coll.find()
Deletion of documents in a collection also is typically done through your Node.js application. However, at
times you may need to manually remove a document froman administrative point of view to fix a database or
for testing purposes.
To remove documents from a collection, you need to get the collection object and then call the remove(query)
method on that object. The optional query parameter specifies a query document with fields and values to
match documents against in the collection. The documents that match the query are removed from the
collection. Using the remove() method with no query parameter removes all documents in the collection. As
an example, the following commands first remove documents where the vehicle is plane and then all
documents from the collection, asshown in Figure 12.11:
use testDB
coll = db.getCollection("newCollection")coll.find()
coll.remove({vehicle: "plane"})coll.find()
coll.remove()
coll.find()
Updates of documents in a collection should also be done through your Node.js application. However, at
times you may need to manually update a document froman administrative point of view to fix a database or
for testing purposes.
To update documents in a collection, you need to get the collection. Then you can use a couple of different
methods: The save(object)method saves changes thatyou have made to an object, and the update(query,
update, options) method queries for documents in the collection and then updates them as they are found.
When using the update() method, the query parameter specifies a query document with fields and values to
match documents against in the collection. The update parameter is an object that specifies the update
operator to use when making the update. For example, $incincrements the value of the field, $setsetsthe value
of the field, $push pushes an item onto an array, and so on. For example, the following update object
increments one field, sets another, and then renames a third:
{ $inc: {count: 1}, $set: {name: "New Name"}, $rename: {"nickname": "al
// Connect to MongoDB
mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('MongoDB connected successfully');
})
.catch(err => {
console.error('MongoDB connection error:', err);
});