0% found this document useful (0 votes)
11 views26 pages

FSD NOTES UNIT-3-1

NoSQL databases, designed for unstructured and semi-structured data, offer flexibility, scalability, and high performance compared to traditional relational databases. They are essential for handling big data, supporting distributed systems, and providing dynamic schema capabilities. MongoDB, a popular NoSQL database, stores data in JSON-like documents and supports various operations such as CRUD, indexing, and aggregation.

Uploaded by

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

FSD NOTES UNIT-3-1

NoSQL databases, designed for unstructured and semi-structured data, offer flexibility, scalability, and high performance compared to traditional relational databases. They are essential for handling big data, supporting distributed systems, and providing dynamic schema capabilities. MongoDB, a popular NoSQL database, stores data in JSON-like documents and supports various operations such as CRUD, indexing, and aggregation.

Uploaded by

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

Unit-III

NoSQL:
NoSQL, or “Not Only SQL,” is a database management system
(DBMS) designed to handle large volumes of unstructured and semi-
structured data.
Unlike traditional relational databases (RDBMS), NoSQL databases
provide flexibility, scalability, and high performance for modern
applications.
Need of NoSQL:
Why NoSQL is Needed?
1. Handling Big Data
 Traditional relational databases struggle to handle large volumes
of data generated by applications such as social media, IoT, and
e-commerce.
 NoSQL databases can store and manage massive amounts of
data efficiently.
2. Scalability
 RDBMS: Typically scales vertically (adding more power to a
single server).
 NoSQL: – NoSQL databases can handle large amounts of data
by distributing it across multiple servers (horizontal scaling). In
contrast, RDBMS usually requires upgrading a single powerful
server (vertical scaling), which is costly and limited.
3. Flexible Schema
 RDBMS follows a strict table structure, which can be difficult to
modify.
 NoSQL allows schema-less data storage, means NoSQL
databases do not have a fixed schema like RDBMS. You can
store data in various formats (JSON, key-value pairs,
documents, graphs) without defining a strict table structure. This
makes it easy to modify and adapt to changing data
requirements.
4. High Performance:
 NoSQL databases are optimized for fast read and write
operations, making them suitable for applications that need
real-time data access, such as social media platforms, gaming,
and big data applications.

5. Handling Unstructured & Semi-structured Data


 Traditional databases work well with structured data (tables,
rows, and columns).
 NoSQL is designed for JSON, XML, key-value pairs, graphs,
and documents, making it suitable for real-world applications.
6. Support for Distributed Systems
 Many NoSQL databases work in a distributed manner, meaning
data is spread across multiple servers.
 This ensures fault tolerance and high availability, reducing
system downtime.
Types of NoSQL Databases
NoSQL databases are generally classified into four main categories
based on how they store and retrieve data
1. Document databases
Store data in JSON, BSON, or XML format.
 Data is stored as documents that can contain varying attributes.
 Examples: MongoDB, CouchDB, Cloudant
 Ideal for content management systems, user profiles, and
catalogs where flexible schemas are needed.
2. Key-value stores
 Data is stored as key-value pairs, making retrieval extremely
fast.
 Optimized for caching and session storage.
 Examples: Redis, Memcached, Amazon DynamoDB
 Perfect for applications requiring session management, real-
time data caching, and leaderboards.
3. Column-family stores
 Data is stored in columns rather than rows, enabling high-
speed analytics and distributed computing.
 Efficient for handling large-scale data with high write/read
demands.
 Examples: Google Bigtable,Apache Cassandra, HBase
 Great for time-series data, IoT applications, and big data
analytics.
4. Graph databases
 Data is stored as nodes and edges, enabling complex
relationship management.
 Best suited for social networks, fraud detection, and
recommendation engines.
 Examples: Neo4j, Amazon Neptune, ArangoDB
 Useful for applications requiring relationship-based
queries such as fraud detection and social network analysis.

Difference between SQL And NoSQL:


SQL (Relational
Feature NoSQL (Non-Relational DB)
DB)
Structured, Flexible (Documents, Key-
Data Model
Tabular Value, Graphs)
Scalability Vertical Scaling Horizontal Scaling
Schema Predefined Dynamic & Schema-less
ACID
Strong Limited or Eventual Consistency
Support
Transactional
Best For Big data, real-time analytics
applications
MySQL,
Examples PostgreSQL, MongoDB, Cassandra, Redis
Oracle

MongoDB:
It is developed and supported by a company named 10gen which is now known as
MongoDB Inc.
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.
Database
 Database is a container for collections.
 Each database gets its own set of files.
 A single MongoDB server can has multiple databases.
Collection
 Collection is a group of documents.
 Collection is equivalent to RDBMS table.
 A collection consist inside a single database.
 Collections do not enforce a schema.
 A Collection can have different fields within a Documents.

A simple MongoDB document Structure:


{
"_id": ObjectId("603d9a0f2f1b6b8a6d3e5b5f"),
"name": "John Doe",
"age": 25,
"email": "[email protected]",
"address": {
"city": "New York",
"zip": "10001"
}
}
Why MongoDB is needed?
There are so many efficient RDBMS products available in the market but Modern
applications are more networked, social and interactive than ever require storing more
and more data and are accessing it at higher rates.This is satisfied by the document
based database like MongoDB.
Features of MongoDB:
Document-Oriented: Stores all related data in a single document instead of breaking it into
multiple tables like in relational databases. For example, all details of a computer are stored
in one document instead of separate tables for CPU, RAM, etc.
Indexing: Uses indexes to speed up searches, avoiding the need to scan every document in a
collection.
Scalability: Expands easily by distributing data across multiple servers with the help of
sharding. Sharding means to distribute data on multiple servers. New servers can be added as
needed.
Replication & High Availability: Keeps multiple copies of data on different servers to
prevent data loss and ensure access even if one server fails.
Aggregation: Processes data and provides summarized results (like sum, average, min, max),
similar to SQL’s GROUPBY clause.
Rich queries: MongoDB supports complex queries with a variety of operators, allowing you
to retrieve, filter, and manipulate data in a flexible and powerful manner.
SQL vs MongoDB:

SQL Terms MongoDB Terms

Databases Databases

Tables Collections

Rows Documents

Columns Fields

Collection
Consider a student databse: { "_id": 1,
"name": "Anitha",
SQL "address": "HNK" Document1
}
Rolln
name Address {
o
"_id": 2,
1 Anitha HNK "name": "Sunitha",
2 Sunitha WGL "address": "WGL"
3 Pranitha KZP }
Datatypes in MongoDB: {
"_id": 3,
"name": "Pranitha",
"address": "KZP"
}
Integer: This datatype is used to store the numerical values.
Boolean: This datatype is used for implementing the Boolean values i.e true or false.
Double: This datatype is used for storing floating point data.
String: This is most commonly used datatype used for storing the string values.
Arrays: For storing an array or list of multiple values in one key ,this datatype is used.
Null: For storing null values this datatype is used.
Date: This datatype is used to store current date or time.We can also create our own date or
time object.
Object: The object is implemented for embedded documents.
Symbol: This datatype is similar to string datatype.This datatype is used to store specific
symbol type.
Binary: In order to store binary data we need to use this datatype.
Regular expression: This datatype is used to store regular expression.

MongoDB – Database, Collection, and Document


In MongoDB, Databases, Collections, and Documents are the fundamental building blocks
for data storage and management.
1)Database: it is a container for collections. A MongoDB database holds multiple
collections that store documents in the form of JSON-like data (BSON format).
MongoDB allows the creation of multiple databases on a single server, enabling efficient
data organization and management for various applications.

CRUD operations
Create Operation:
a) Database Creation: Databases are created when you insert data into them. You can create
or switch to a database using the following command:
Syntax:
use <database_name>
Example: use Vaagdevi
switched to db Vaagdevi
Vaagdevi>
b) Show dbs: To see all available databases.
test> show dbs
config 72.00 KiB
grid 89.48 MiB
gridfs 89.46 MiB
local 72.00 KiB
Create or insert operations add new documents to a collection. If the collection does not
currently exist, insert operations will create the collection.
MongoDB provides the following methods to insert documents into a collection:
 db.collection.insertOne()
 db.collection.insertMany()

a)Creating collection
After creating database now we create a collection to store documents.
syntax:
db.createCollection("your_collection_name")
Note:
In MongoDB, collections are usually created automatically when you insert the first
document into them.
b) insertOne() function is used to store single data in the specified collection. And in the
curly braces {} we store our data or a document.

Syntax:
db.collection_name.insertOne({..})
Example:
db.employee.insertOne({empid:1,name:"nelli sathish",designation:"it
employee",qualification:"m.tech"})
{
acknowledged: true,
insertedIds: { '0': ObjectId('66e596036de17b79bbc73bf9') }
}
c) insertMany():To insert multiple documents at once.

db.employee.insertMany([{empid:2,name:"sravanthi jakkula",designation:"assistant
professor",qualification:"m.tech",phone:1234566778},{empid:3,name:"vasundhara
nam",designation:"assistant professor",qualification:"m.tech",address:"hyderabad"}])
{
acknowledged: true,
insertedIds:
{
'0': ObjectId('66e599596de17b79bbc73bfa'),
'1': ObjectId('66e599596de17b79bbc73bfb')
}
}
d) Show collections: To see all available collecions in database.
Syntax:
test> show collections

Read Operations
Read operations retrieve documents from a collection; i.e. query a collection for documents.

e) find():Find all documents in a collection


Syntax:
db.collection.find()
Example:
db.student.find()
[
{
_id: ObjectId('67d1a4a7bd48f9f1dbb71236'),
rollno: 123,
name: 'Anitha'
},
{
_id: ObjectId('67d851da5f5f5daed5b71238'),
name: 'prashanthi',
fathername: 'rammallu',
mailid: '[email protected]'
}
f) findOne(): It retrieve a single document from a collection that matches a specified query. It
returns the first document that matches the criteria (or null if no matching document is
found).
Syntax:
db.collection.findOne({Query})
Example:
db.student.findOne({name:"anitha"})
null
vaagdevi> db.student.findOne({name:"prashanthi"})
{
_id: ObjectId('67d851da5f5f5daed5b71238'),
name: 'prashanthi',
fathername: 'rammallu',
mailid: '[email protected]'
}

Update Operations
Update operations modify existing documents in a collection. MongoDB provides the
following methods to update documents of a collection:
 db.collection.updateOne()-Updates a single document that matches the query.
 db.collection.updateMany()– Updates multiple documents that match the query.
 db.collection.replaceOne() – Replaces a single document with a new one

updateOne():
Example:
db.student.updateOne({name:"prashanthi"},{$set:{"fathername":"rammallu"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

Find():db.student.find()
[
{
_id: ObjectId('67d1a4a7bd48f9f1dbb71236'),
rollno: 123,
name: 'Anitha'
},
{
_id: ObjectId('67d851da5f5f5daed5b71238'),
name: 'prashanthi',
fathername: 'rammallu',
mailid: '[email protected]'
}
]
updateMany:
Example:
vaagdevi> db.student.find();
[
{
_id: ObjectId('67d1a9a0bd48f9f1dbb71237'),
rollno: 123,
name: 'Anitha',
age: 20,
branch: 'ece',
Mobileno: 123456
},
{
_id: ObjectId('67d850e05f5f5daed5b71236'),
name: 'Pranitha',
branch: 'ECE'
},
{
_id: ObjectId('67d851da5f5f5daed5b71237'),
name: 'Pranitha',
branch: 'ECE'
},
{
_id: ObjectId('67d851da5f5f5daed5b71238'),
name: 'prashanthi',
fathername: 'rammallu',
mailid: '[email protected]'
},
{
_id: ObjectId('67db61608e3f40f738b71236'),
name: 'Ranjith',
branch: 'cse'
},
{
_id: ObjectId('67db61608e3f40f738b71237'),
name: 'Rama',
branch: 'cse'
}
]
vaagdevi> db.student.updateMany({branch:"cse"},{$set:{branch:"ece"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
vaagdevi> db.student.find();
[
{
_id: ObjectId('67d1a9a0bd48f9f1dbb71237'),
rollno: 123,
name: 'Anitha',
age: 20,
branch: 'ece',
Mobileno: 123456
},
{
_id: ObjectId('67d850e05f5f5daed5b71236'),
name: 'Pranitha',
branch: 'ECE'
},
{
_id: ObjectId('67d851da5f5f5daed5b71237'),
name: 'Pranitha',
branch: 'ECE'
},
{
_id: ObjectId('67d851da5f5f5daed5b71238'),
name: 'prashanthi',
fathername: 'rammallu',
mailid: '[email protected]'
},
{
_id: ObjectId('67db61608e3f40f738b71236'),
name: 'Ranjith',
branch: 'ece' //updated
},
{
_id: ObjectId('67db61608e3f40f738b71237'),
name: 'Rama',
branch: 'ece' //updated
}
]
Delete Operation:

deleteOne():This method accept a query object. The


matching documents will be deleted.
Example:
vaagdevi> db.student.deleteOne({name:"Anitha"});
{ acknowledged: true, deletedCount: 1 }
vaagdevi> db.student.find();
[
{
_id: ObjectId('67d1a9a0bd48f9f1dbb71237'),
rollno: 123,
name: 'Anitha',
age: 20,
branch: 'cse',
Mobileno: 123456
},
{
_id: ObjectId('67d850e05f5f5daed5b71236'),
name: 'Pranitha',
branch: 'ECE'
},
{
_id: ObjectId('67d851da5f5f5daed5b71237'),
name: 'Pranitha',
branch: 'ECE'
},
{
_id: ObjectId('67d851da5f5f5daed5b71238'),
name: 'prashanthi',
fathername: 'rammallu',
mailid: '[email protected]'

}
]

Administering User Accounts:


Mongodb stores the information of all the users
including name, password and authentication in the
system.
An administrative user account has higher levels of
access and can manage databases, user permissions, and
system-level configurations. Administering user
accounts in MongoDB allows an administrator to
control access to the database, assign roles, and perform
administrative tasks like creating, modifying, or
deleting users.
MongoDB uses a role-based access control system,
where users are granted specific roles that define their
access level to databases and actions they can perform.
Using MongoDB shell command we can create the user
account. Using such accounts we can read and write to
the database.
The most commonly used operations are:
1)List the users
2) create the user accounts
3) Remove the users
Listing users:
For listing the users in the system , switch to
admin database and then use show users
command.
User accounts are stored in db.system.users
collection of each database. There exists an object
named User which contains the fields like
_id,name,pwd and roles.
Example:
test> use admin
switched to db admin
show collections: Listing All Collections in the
admin Database
To see all collections in the admin database (which
contains system collections like system.users)
admin> show collections
system.users
system.version
db.system.users.find():
To view all users in the admin database use
command db.system.users.find().
This shows the user information stored for
authentication and authorization.
Output:
[
{
_id: 'admin.Anitha123',
userId: UUID('7a9dde51-b0fa-4d52-b934-da5ee7a9030f'),
user: 'Anitha123',
db: 'admin',
credentials: {
'SCRAM-SHA-1': {
iterationCount: 10000,
salt: 'KUsyn/X5ojWltL/kzZSHzg==',
storedKey: 'E1RI51ty8KeWUr1rSH+zTaFv2Dw=',
serverKey: 'nHO38duXRDYJR+RIScKLxwTBOQg='
},
'SCRAM-SHA-256': {
iterationCount: 15000,
salt: 'Q5ByNREX7V59XKCoW6J8RNdsU5UNcj0ID4JwcA==',
storedKey: 'wH2xdR6GCJep8V1OMojvZU7ECNWqhJSgI6CzP0t9FL4=',
serverKey: 'aDFvtEIzWL9Ce2hDn7gENcL2bovNZ0ShKD9ec/NxsUs='
}
},
roles: [ { role: 'root', db: 'admin' } ]
},
{
_id: 'admin.Anitha1234',
userId: UUID('507df6df-42ec-4557-9e66-70141ca0c7e7'),
user: 'Anitha1234',
db: 'admin',
credentials: {
'SCRAM-SHA-1': {
iterationCount: 10000,
salt: 'Jj4z4UpNELo6YGV6x23lTA==',
storedKey: 'TKa/xGvzy6r4VO86I6GOHgEZYlE=',
serverKey: 'vQiY9t78RDFAYw8gS4rQzc6SSLA='
},
'SCRAM-SHA-256': {
iterationCount: 15000,
salt: 'mwOy568T/UT588uVctSy4cUrMEyb5yM3FQTKkA==',
storedKey: 'uPz07leQbd3RItBdgHa+tDp2LJHdecLERWYu9CNvUkQ=',
serverKey: '+xYdqIXCBj6jP8MuPSdLO8Wl4Muejlr1xuwqAUwqmlw='
}
},
roles: [
{ role: 'root', db: 'admin' },
{ role: 'readWrite', db: 'admin' }
]
}
(Or)
db.getUsers():
To view all users in the admin database use command db.getUsers().

Creating a New User


createUser() :To create a new user in the admin database, use the createUser command.
This allows you to assign roles and define access permissions for the new user.
Example:
admin>db.createUser({user:"pranitha",pwd:"1234",roles:[{role:"readWrite",db:"admin"}]})
{ ok: 1 }
Now to test if user named “Pranitha” is created or not . We use show users command
admin> show users
[
{
_id: 'admin.pranitha',
userId: UUID('a9bbb4f4-0d24-4623-9916-2a65bf39cb76'),
user: 'pranitha',
db: 'admin',
credentials: {
'SCRAM-SHA-1': {
iterationCount: 10000,
salt: 'NYO9LEQivbmfDGMLJq2Gkg==',
storedKey: 'mMQAbvcGUZZlsjnk3mNyywOceOA=',
serverKey: '8HmpxYbruhYYB2PifNO/Nn1cSl8='
},
'SCRAM-SHA-256': {
iterationCount: 15000,
salt: 'DfeT5zTWa68huJcxIj+7K9lMQVjskEz5N0Wfkg==',
storedKey: 'A/PQvcggVUla+UFM+/IWIb5pbDnaoOSmnU9k0mVki04=',
serverKey: 'Dt8CkoJXptaUjBu1q0W1Ifc/8LCnap3jNTpNgfTI5IY='
}
},
roles: [ { role: 'readWrite', db: 'admin' } ]
}
]
Creating the user with roles:
The following are the some commonly used roles that can be assigned to the user accounts.
Role Description Permissions
Grants read-only access to a
Read Read data from collections.
database.

Grants read and write access to a - Read and write data (insert,
readWrite
database. update, delete) in collections.

- View and modify indexes.


Grants administrative access to
dbAdmin manage database structure and - View database statistics.
settings.
- Manage collection settings.

- Create, drop, and modify


Grants access to manage users users.
userAdmin
and roles within a database.
- Grant or revoke roles to users.

Grants read access to all databases


readAnyDatabase - Read data from any database.
in the MongoDB instance.
Grants read and write access to all
- Read and write data to any
readWriteAnyDatabase databases in the MongoDB
database.
instance.

- Full access to all databases.

Grants full access to all actions


Root across all databases and - Perform any action: read,
administrative tasks. write, admin tasks, user
management, and more.

- Full access to a specific


Grants full access to a specific database: managing
dbOwner
database, including management. collections, indexes, and roles
within the database.

Remove users:
For removing the user , the db.dropUser() method is used.
Syntax:
db.dropUser(username)
Example
db.dropUser("pranitha")
{ ok: 1 }
Configuring access control:
Securing our database is very important to protect our data.
MongoDB, provides robust authentication mechanisms and role-based access control
(RBAC) features to secure data and manage user privileges effectively.

Role-based access control (RBAC):


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).
Enabling authentication in MongoDB ensures that only authorized users can interact with the
database, keeping our data safe.
Authentication: Is the process of verifying the identity of users or applications attempting to
access the database. It ensures that only authorized individuals can access sensitive data by
requiring them to provide valid credentials such as a username and password.
Why is Authentication Important in MongoDB?
 Prevents Unauthorized Access: Ensures that only users with valid credentials can
access and modify the database.
 Protects Sensitive Data: Helps safeguard sensitive information such as customer
data, financial records, and intellectual property.
 Ensures Data Integrity: Reduces the risk of accidental or malicious data
tampering or loss.
Authorization: determines what an authenticated user is allowed to do once they have been
authenticated. It defines the permissions or actions a user can perform on resources
(databases, collections, etc.) within MongoDB.

Enabling Authentication in mongodb:


MongoDB supports authentication to ensure that only authorized users can access and
perform operations on the database. Enabling authentication involves two main steps:
1. Start MongoDB with Authentication Enabled
2. Create an Administrative User
Steps to Enable Authentication:
1. Start MongoDB with the --auth command to enable authentication.
Open cmd type
mongod –auth
This command starts the MongoDB server and enables authentication, meaning that
only users with valid credentials can access the database.
(Or)
2.you can modify the MongoDB configuration file (mongod.conf) to enable
authentication permanently:
 Open the mongod.conf file.
To open mongod.conf file Go to C:\Program Files\MongoDB\Server\8.0\bin
 Find the security section and add the following:
security:
authentication: "enabled"
3: Restart MongoDB for changes to take effect.
Goto Services
2.Creating an Administrative User:
After enabling authentication, you need to create a user with administrative privileges to
manage other users.
Steps to Create an Admin User:
Step 1: Connect to the MongoDB shell.
Open cmd
> mongosh
Step 2: Switch to the admin database
use admin
Step 3: Create an administrative user with the userAdminAnyDatabase role.
This role allows the user to create and manage other users.
db.createUser({
user: "sysadmin",
pwd: "123456",
roles:[{role:"userAdminAnyDatabase.db:”admin”}]
})
Note that now the administrator user is now sysadmin with password 123456
Step 4:we can authenticate the user by using db.auth command with username and
password.
Admin>db.auth(“sysadmin”,”123456”)
Creating Database Administrator Account:
createUser method is used for creating a Database Administrator account. Then assign the
roles such as readWriteAnyDatabase, dbAdminAnyDatabase.

db.createUser({
user: "swapna",
pwd: "123456",
roles: [ { role: " readWriteAnyDatabase ", db: "admin" }]})
The readWriteAnyDatabase role grants full access across all databases.
In mongodb shell we can use this user to administer the database.

Managing Collections:
After creating database we must create collection inside that database.We can perform
various operations such as insert ,delete,update on this collection.These operations are called
CRUD opertions.

Week-6 Program

Connecting to mongodb from nodejs:


For connecting the Node.js with mongodb we use MongoClient

Following are the steps used to connect Node.js with Mongodb


Step1: Install Mongodb driver for Node.js
npm install mongodb
Step2: In the Nodejs application programs require the Mongodb module.
const { MongoClient } = require('mongodb');
MongoClient: This is the class used to connect to MongoDB databases. It provides methods
to establish a connection, interact with the database, and close the connection .

Step3: Define Mongodb URL


const uri = 'mongodb://localhost:27017';
defines the MongoDB connection URI (Uniform Resource Identifier), which specifies how to
connect to the MongoDB server.

Step4: Create the instance of MongoClient by passing above URL to it.


const client = new MongoClient(uri);

Step5: Finally use connect method to get connect with the database
client.connect();
week-7 programs

Understanding the objects used in Mongodb Node.js Driver


MongoDBClient:
This object provides interactions to connect to the database.
Methods of MongodbClient object
Methods Purpose

This method is used to connect to the


connect(url) mongodb using the specified url
It creates anew DB instance sharing
db(dbName) the current connections
isConnected() It checks if MomgoClient connected
this method closes the db underlying
close() connections

The db object:
The db objectis created for connecting to the database in MongoDB.
Methods of db object:
Method Purpose
collection(name) Access a collection in the database
createCollection(name,
options)
Create a new collection
dropCollection(name) Delete a collection from the database
listCollections(filter,
options)
List all collections in the database
Execute a database command (e.g.,
command(command)
stats)
dropDatabase() Delete the entire database
getCollection(name) Get a collection (alias to collection())

const { MongoClient } = require('mongodb');


const client = new MongoClient('mongodb://localhost:27017');

async function run() {


await client.connect();
const db = client.db('testdb');

// Create a collection
await db.createCollection('users');

// Insert a document
const users = db.collection('users');
await users.insertOne({ name: 'John', age: 28 });

// Query documents
const user = await users.findOne({ name: 'John' });
console.log(user);

// List collections
const collections = await db.listCollections().toArray();
console.log(collections);

// Get database stats


const stats = await db.stats();
console.log(stats);

// Drop the collection


await db.dropCollection('users');

await client.close();
}
run();

You might also like