If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
अभी तक Node.js के साथ MySQL database के साथ practice की इस topic में हम MongoDB database के साथ connection कैसे बनाते हैं , और कैसे use करते हैं , वो देखेंगे।
Node.js में MySQL के साथ - साथ MongoDB database भी easily connect कर सकते हैं। इसके लिए सबसे पहले तो mongodb package install करना पड़ेगा।
MongoDB , high volume data को store करने के लिए document-oriented NoSQL database है। NoSQL means इसमें Dababase से data fetch करने लिए MySQL की तरह SQL Queries नहीं होती हैं। यह Data को BSON (Binary JSON) format में store करता है।
MongoDB , MySQL के comparison में अलग तरह से तो data store करता है। साथ ही इनके terms भी अलग होते हैं जैसे -
MySQL Database , set of tables होता है। जबकि MongoDB set of collections होता है।
means , table को collection कहते हैं।
और rows को documents कहते हैं।
npm install mongodb
Package Install हो जाने के बाद , आप इस कुछ इस तरह से use कर सकते हैं।
const mongoClient = require('mongodb').MongoClient
मैं यह पर , Online MongoDB Cloud Services use कर रहा हूँ , हालाँकि आप अपने system पर भी MongoDB install कर सकते हैं।
// import MongoClient from mongodb package
const mongoClient = require('mongodb').MongoClient;
// use local mongdb if you have installed it on your system.
// const url = "mongodb://localhost:27017/node_mongdb";
const url = "mongodb+srv://<username>:<password>@cluster0.2n7fz.mongodb.net/node_mongdb?retryWrites=true&w=majority";
// make connection
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true , connectTimeoutMS: 30000 , keepAlive: 1});
client.connect(error => {
if(error){
console.log("MongoDB Connect Error", error.name, error.message);
}else{
console.log("Database created");
// close connection
client.close();
}
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js Database Created
ऊपर दिए गए example को run करने पर node_mongdb database create हो जायगा। हालाँकि अभी database अभी दिखेगा नहीं जब तक उसमे एक table (collection) नही होगी।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook