「Node.js」updateMany()関数でMongoDBのコレクションの複数データを更新する

2020年11月23日

構文
dbo.collection(“testcollect").updateMany(whereStr, updateStr, function(err, res
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("arkgamedb");
   // 条件検索
var whereStr = {"type":'enver'};
var updateStr = {$set: { "url" : "https://www.arkgame.com" }};
dbo.collection("testcollect").updateMany(whereStr, updateStr, function(err, res) {
if (err) throw err;
console.log(res.result.nModified + " collection are updated");
db.close();
});
});
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("arkgamedb");    // 条件検索 var whereStr = {"type":'enver'}; var updateStr = {$set: { "url" : "https://www.arkgame.com" }}; dbo.collection("testcollect").updateMany(whereStr, updateStr, function(err, res) { if (err) throw err; console.log(res.result.nModified + " collection are updated"); db.close(); }); });
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
 
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("arkgamedb");
   // 条件検索
    var whereStr = {"type":'enver'}; 
    var updateStr = {$set: { "url" : "https://www.arkgame.com" }};
    dbo.collection("testcollect").updateMany(whereStr, updateStr, function(err, res) {
        if (err) throw err;
         console.log(res.result.nModified + " collection are updated");
        db.close();
    });
});

 

Node.js

Posted by arkgame