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:
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
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
Recent Comments