Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
72 views3 pages

Experiment 9

This document provides instructions on connecting MongoDB to a Node.js application. It explains how to install MongoDB and the MongoDB driver for Node.js, and demonstrates how to create a database and collection. The objectives are for students to learn how to connect MongoDB with Nodejs. It also includes an exercise to create a Node.js application that connects to MongoDB.

Uploaded by

darshana patel
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)
72 views3 pages

Experiment 9

This document provides instructions on connecting MongoDB to a Node.js application. It explains how to install MongoDB and the MongoDB driver for Node.js, and demonstrates how to create a database and collection. The objectives are for students to learn how to connect MongoDB with Nodejs. It also includes an exercise to create a Node.js application that connects to MongoDB.

Uploaded by

darshana patel
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/ 3

EXPERIMENT NO: 9 DATE: / /

TITLE: MongoDB with NodeJs

OBJECTIVES: On completion of this experiment students will be able to…


➢ know the concept of Connectivity of MongoDB with Nodejs

THEORY:

Install MongoDB :

You can download a free MongoDB database at https://www.mongodb.com.

Or get started right away with a MongoDB cloud service at


https://www.mongodb.com/cloud/atlas.

Let us try to access a MongoDB database with Node.js.

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

Note: First open another terminal an run command mongod to start your mongoDB

Node.js MongoDB Create Database

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {

if (err) throw err;

console.log("Database created!");
db.close();

});

C:\Users>node demo_create_mongo_db.js

Node.js MongoDB Create Collection:

A collection in MongoDB is the same as a table in MySQL

Creating a Collection

To create a collection in MongoDB, use the createCollection() method:

Example
Create a collection called "customers":

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {

if (err) throw err;

var dbo = db.db("mydb");

dbo.createCollection("customers", function(err, res) {

if (err) throw err;

console.log("Collection created!");

db.close();

});

});

Page 2 of 3
EXERCISE:

1.Create Node.js Application to Connect Node JS with MongoDB

EVALUATION:

Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)

Signature with date: ________________

Page 3 of 3

You might also like