「Node.js」updateOne関数でMongoDBのコレクションの1件データを更新する

2020年11月23日

構文
変数.collection(collection名).updateOne(条件, 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 = {"name":'arkgame'};
var updateStr = {$set: { "url" : "https://www.arkgame.com" }};
dbo.collection("testtbl").updateOne(whereStr, updateStr, function(err, res) {
if (err) throw err;
console.log("collection update success");
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 = {"name":'arkgame'}; var updateStr = {$set: { "url" : "https://www.arkgame.com" }}; dbo.collection("testtbl").updateOne(whereStr, updateStr, function(err, res) { if (err) throw err; console.log("collection update success"); 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 = {"name":'arkgame'};  
    var updateStr = {$set: { "url" : "https://www.arkgame.com" }};
    dbo.collection("testtbl").updateOne(whereStr, updateStr, function(err, res) {
        if (err) throw err;
        console.log("collection update success");
        db.close();
    });
});

 

Node.js

Posted by arkgame