0% found this document useful (0 votes)
43 views

Https Github Com Prasadkaru MongoDB Lesson Code 1704253560

The document discusses MongoDB and how it compares to relational databases. It provides information on connecting to MongoDB, inserting and querying data, and using MongoDB with Node.js drivers and Mongoose.

Uploaded by

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

Https Github Com Prasadkaru MongoDB Lesson Code 1704253560

The document discusses MongoDB and how it compares to relational databases. It provides information on connecting to MongoDB, inserting and querying data, and using MongoDB with Node.js drivers and Mongoose.

Uploaded by

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

MONGO DB

INNOWAVE TEAM
(100+ SKILLED RESEARCHERS POOL)
"BRIDGING THE GAP BETWEEN KNOWLEDGE AND
APPLICATION. YOUR MULTI-DISCIPLINARY RESEARCH
PARTNER."

Contact US :
+94 70 225 2557 (WhatsApp)
HELPING ALL TYPE OF UNDERGRADES FOR COMPLETING
THEIR PROJECTS AND ASSIGNMENTS
Feature Relational Databases Non-Relational Databases

Various: key-value, document, column-


Data Structure Tables with rows and columns
family, graph

Relationships Defined using foreign keys Implicit or handled separately

Schema Fixed before data entry Flexible or schema-less

Vertical scaling (adding resources to a Horizontal scaling (distributing data


Scalability
single server) across multiple servers)

Querying SQL Varies by model

Consistency Strong ACID guarantees Varies by implementation

Examples MySQL, PostgreSQL, Oracle MongoDB, Cassandra, Redis, Neo4j

Structured data, predictable relationships, Large datasets, unstructured data,


Best for
data integrity flexibility, scalability

WHAN BEFORE USING RELATIONAL DATABASES

Create DB
SQL USE FOR CRUD RELATIONAL DATABASES

CRUD SQL HTTP

Create INSERT POST

Read SELECT GET

Update UPDATE PUT

Delete DELETE DELETE

MONGODB
MongoDB is a document database and can be installed
locally or hosted in the cloud.

The MongoDB Query API can be used two ways:


CRUD Operations
Aggregation Pipelines

Inserting Documents

two ways intert data


1.mongosh
2mongodb driver
why we use npm init -yes before working

Project Initialization:

Creates a package.json file, essential for managing Node.js projects.


Stores project metadata (name, version, author, etc.).
Tracks project dependencies, ensuring consistent environments across
machines
.
Dependency Management:

Prepares the project for installing MongoDB drivers or tools.


npm install command relies on package.json to manage dependencies.
The command npm i mongodb will install the official Node.js driver for
interacting with MongoDB into your current project directory. Here's
what happens:

Download: It downloads the mongodb package and its dependencies


from the npm registry.
Installation: The downloaded files are placed in the node_modules
directory within your project.
Dependency Recording: An entry for the mongodb package is added to
your package.json file, which keeps track of all your project's
dependencies.
It's important to note that:

You need to be in the directory of your Node.js project when running


the command.
Ensure you have Node.js and npm installed on your system.
Running this command without previous project initialization (using
npm init -y) will still work, but it's good practice to have a package.json
file for managing dependencies.
Connect to MongoDB

1. Choose Your Method:

Mongo Shell (mongosh): A command-line interface for direct interaction.


Node.js Driver: For integrating MongoDB into Node.js applications.
MongoDB Compass: A GUI for visual interaction and data management.
Other Drivers and Tools: MongoDB supports drivers for various programming
languages and platforms.

2. Obtain Connection Information:

Local MongoDB Instance:


Default host: localhost
Default port: 27017
Remote MongoDB Instance:
Obtain host name or IP address.
Verify port number (usually 27017).
May require authentication credentials.
MongoDB Atlas Cluster:
Get connection string from Atlas dashboard.
5. Connect Using MongoDB Compass:

1. Download and install MongoDB Compass.


2. Launch Compass and create a new connection.
3. Paste connection string or provide host, port, authentication details.
4. Click "Connect".
connect database

Insert Data

Create database

const database = client.db('myDatabase');

Create collection

const collection = database.collection('products')

There are 2 ways to create a collection.


insert a value
const doc = {
name:'abc',
price:100.00,
brand : "a"
}
const result = await collection.insertOne(doc);
console.log(result)
There are 2 methods to insert documents into a MongoDB database.
insert more value
Find Documents
Find Data
There are 2 methods to find and select data from a MongoDB collection,
find() and findOne().

find all the product - findAllProducts()


find the first product - findFirstProducts()

Querying Data
To query, or filter, data we can include a query in our find() or findOne()
methods
find all product with name - findByName(name)
find all the products with qty-findByQty(qty)
find all the products that rated user name-findByRateUser(user)
sort all products by price-sortByPrice()

filter name and after sort


filter fields in all products-filterFields()

Both find methods accept a second parameter called projection.


find all products name start with "Laptop"-findAllProductsStartsWithLaptop()

Without case sensative


//find all products name contain-findAllProductsContain(name)
//find all products name contain end-findAllProductsEndWith(name)
//sort and filter all products by price,stats with -filterSortByPriceStartWith(name)
Update and Delete Documents

To update an existing document we can use the updateOne() or


updateMany() methods.
replace
Delete Documents

We can delete documents by using the methods deleteOne() or deleteMany().


Mongoose is an object-document mapping (ODM)
framework for Node.js and MongoDB
Connect atlas cloud
Mongoose Schema and model
Document
schema connect
Contact US :

+94 70 225 2557


HELPING ALL TYPE OF UNDERGRADES FOR COMPLETING
THEIR PROJECTS AND ASSIGNMENTS

You might also like