JavascriptMongodbNodejs

How to connect with Mongodb with Nodejs and fetching data?

How to connect with Mongodb with Nodejs and fetching data
196Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to connect with Mongodb with Nodejs and fetching data?

MongoDB is NoSQL database. Now I am learning this and this is very interesting and I am going to share some working examples.

We can also save the mongodb data into custom json file and this feature makes mongodb better.

Mongodb data with command prompt query:

 

mongodb data with command prompt query

Here is the working coding steps you need to follow:

1. First you need to run below query to install mongodb into your node app:

$ npm install mongodb

 

2 . Here is the code you need to add into testcode.js(you can choose you name) file:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("test");
  dbo.collection("test").findOne({}, function(err, result) {
    if (err) throw err;
    console.log(result.Field);
    db.close();
  });
});

 

3. After this, run below command into your terminal and see the output:

$ node testcode.js
node output with data

Now this is it, if you have any query related to this post then do comment below or ask question.

Thank you,

ludhiana wala ajay,

TheRichPost

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL