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 में MongoDB Database में Collections को update करने के लिए updateMany() और updateOne() method का use किया जाता है। इस topic में आप यही सीखेंगे कि इन methods का use करके किस तरह से Collections को update किया जाता है।
updateOne({}, updateObj, callback) method का use करके Collection में किसी single document / record को update किया जाता है।
whereObj अगर हमें condition के according किसी particular document को update करना है , तो Object में column : value pass कर सकते हैं , by default empty object pass किया जाता है।
updateObj यह भी एक Object होता है , जिसमे $set operator के साथ वो सभी updated values का object assign किया जाता है , जिन्हे हमें update करना है।
callback यह callback function होता है , जिसमे हम success और error handle करते हैं।
const { MongoClient } = require('mongodb');
const uri = "mongodb+srv://dbUser:dbPassword@cluster0.2n7fz.mongodb.net/node_mongodb?retryWrites=true&w=majority";
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{
const database = client.db("node_mongodb");
const users = database.collection("users");
let whereObj = {name : "Rahul Kumar"};
let updateObj = {$set : {age : 40}};
users.updateOne(whereObj, updateObj, function(error, result){
if(error){
console.log("MongoDB Find Error", error.name, error.message);
}else{
console.log(result);
}
})
}
});
Output में आपको हमेशा की तरह ही , एक Object हो return होता है जिसमे modifiedCount की value अगर 0 से ज्यादा है , तो हमारा record / document update हो चुका है। जैसा कि नीचे दिखाया गया है।
C:\Users\HP\Desktop\workspace\nodejs>node app.js { acknowledged: true, modifiedCount: 1, upsertedId: null, upsertedCount: 0, matchedCount: 1 }
updateMany() method का use करके Collection में condition से matched सभी documents / records को update किया जाता है। updateOne() method की तरह यह भी 3 arguments accept करता है, और उनकी working भी same होती है।
const { MongoClient } = require('mongodb');
const uri = "mongodb+srv://dbUser:dbPassword@cluster0.2n7fz.mongodb.net/node_mongodb?retryWrites=true&w=majority";
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{
const database = client.db("node_mongodb");
const users = database.collection("users");
let updateObj = {$set : {website : "learnhindituts.com"}};
users.updateMany({}, updateObj, function(error, result){
if(error){
console.log("MongoDB Find Error", error.name, error.message);
}else{
console.log(result);
}
})
}
});
नीचे Output में clear देख सकते हैं कि matchedCount: 3 , means 3 documents match किये गए हैं , लेकिन modifiedCount: 2 है मतलब , 2 documents update हुए हैं , क्योंकि एक record में already वो value थी।
C:\Users\HP\Desktop\workspace\nodejs>node app.js { acknowledged: true, modifiedCount: 2, upsertedId: null, upsertedCount: 0, matchedCount: 3 }
तो कुछ इस तरह से Node.JS में MongoDB Database में Documents(Rows) को update करते हैं , I Hope, आपको अच्छे से समझ आया होगा।
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