Home Javascript How to connect with Mongodb with Nodejs and fetching data?

How to connect with Mongodb with Nodejs and fetching data?

by therichpost
Published: Updated: 0 comments
import csv to mongodb node js

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

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.