Introduction
Node.js is an open-source, cross-platform and provides a run-time environment for developing server-side and networking applications. Node.js applications are written in JavaScript and can run within Node.js Node.js also provides a rich library of various Javascript modules which simplifies the development of web applications using Node.js
VS Code
VS Code is a source-code editor developed by Microsoft for Windows, Linux, and iOS. The code supports many languages.VS Code intuitive keyboard shortcuts, and easy customization.
Step 1. Download Node.js from the link then install Node js.
Step 2. Download VS Code from this link and install it.
Step 3. Download MongoDB compass from the link to view the database and after downloading install MongoDB and run their service from the web service.
Step 4. Check the version of Node to run the command on cmd “ node -v”.
Step 5. Open VS Code in your system and run a command to install npm “install npm -2 @angular/cli”, Then wait to install the complete package(Library) from the service.
Step 6. After completing the installation, open VS Code in your system and select a folder to click on the Folder then open this folder in VS Code.
MongoDB
Mongo DB is a cross-platform, open-source document-orient database. Mongod is a NoSQL database(no structure). Mongo DB stores JSON data in the form of collections. MongoDB has no structure of the data, so it’s called NoSQL data. Mongo DB has no limit to storing data in a database like SQL. Mongo DB has no requirements to declare the length of the field like SQL/ Oracle / SqlYog.
CRUD Operation in MongoDB
CRUD stands for " create, read, update, and delete". Crud operations are used to create, read, update, and delete any documents.
Connect your system to the internet open the command prompt and then run the command
install mongodb --save
This command is used to install the MongoDB package in your system. It will take more than 2 minutes. When it completes, then close the command prompt
Step 7. Create a database in MongoDB using Node.js and VS Code. first, open VS Code and create a folder where you want to make a database program. and then open this folder in VS Code
Step 8. For performing any crud operation in MongoDB, you need a database and a collection. First. create a database and then create a collection.
Step 9. Create a database
// Create a .js page (createdatabase.js) and now write the code:
createdatabase.js
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, function(error, databases) {
if (error) {
throw error;
}
var dbobject = databases.db('navigcollection');
console.log("databases is created");
databases.close();
});
Now compile this program in the terminal and write a command to compile
node createdb.js
After the compile, you should see that your database looks like this.

Now go to the MongoDB compass and refresh the databases. after refreshing the database you see there is no database in databases.

Because an empty database doesn't show in the database list, to view the database we need to again create a collection in your database. After creating the collection your database is shown in the databases list.
Step 10. Create a collection in the database
// Create a .js file ("createcollection.js") and write code.
createcollection.js
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(error, databases) {
if (error) {
throw error;
}
var dbase = databases.db("navigcollection");
dbase.createCollection("pract", function(error, response) {
if (error) {
throw error;
}
console.log("collection is created.....");
databases.close();
});
});
Now compile this program in the terminal and write the command for compile.
node createcollection.js
After compiling, the output will look like.

Now go to the database and refresh databases. After refreshing, the output will look like this.

Now click on your database and afterwards, click the collection shown,

Step 11. Insert record in the database
Now insert a record in the collection, to insert a record in the database create a new page("insert1docu.js") and write code.
insert1docu.js"
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var url = "mongodb://localhost:27017/";
mongoClient.connect(url, function(err, databases) {
if (err) {
throw err;
}
var nodetestDB = databases.db("navigcollection");
var customersCollection = nodetestDB.collection("pract");
var customer = {_id: 111, name: "Santosh Kumar", address: "B-222, Sector-19, NOIDA", orderdata: "Arrow Shirt"};
customersCollection.insertOne(customer, function(error, response) {
if (error) {
throw error;
}
console.log("1 document inserted");
databases.close();
});
});
Now compile this program in the terminal and write the command.
node insert1docu.js
The output will look like this.

Then open the database and refresh the collection.

Now insert a record into the collection, create again a js file ("createmanydocu.js"), and write the code.
createmanydocu.js
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, function(error, databases) {
if (error) {
throw error;
}
var nodtst = databases.db("navigcollection");
var pract = [
{_id: 11, name: "Chaman Gautam", address: "Harvansh nagar Ghaziabad", orderdata: "Jeans"},
{_id: 12, name: "Shivani", address: "Harvansh nagar Ghaziabad", orderdata: "Jeans"},
{_id: 13, name: "Menu", address: "Harvansh nagar Ghaziabad", orderdata: "Top"},
{_id: 14, name: "Brajbala", address: "Harvansh nagar Ghaziabad", orderdata: "Dining table"},
{_id: 15, name: "Ramsaran", address: "Harvansh nagar Ghaziabad", orderdata: "Washing machine"},
{_id: 16, name: "Dheeraj", address: "Harvansh nagar Ghaziabad", orderdata: "Jeans"}
];
nodtst.collection('pract').insertMany(pract, function(error, response) {
if (error) {
throw error;
}
console.log("Number of documents inserted.........");
});
});

















Deepak NagarPosted Feb 29, 2020, 8:17 AM
Very helpful article thks for sharing
Rushi MehtaPosted Dec 19, 2019, 4:10 AM
Very useful article
Onkar SharmaPosted Dec 18, 2019, 6:38 AM
Very helpful article, thanks for sharing...
Sourav Kumar DasPosted Dec 17, 2019, 10:46 PM
Nice useful article.