[OCI]尝试使用MongoDB客户端连接到Autonomous JSON数据库(Node.js应用程序版本)

首先

これまでの以下の記事で、Oracle Database API for MongoDBを利用することによりMongoDB Shell、mongoexport/mongoimportなどのMongoDBクライアントからAutonomous JSON Databaseを操作できることを確認しました。

[OCI]MongoDBクライアントからAutonomous JSON Databaseに接続してみた(MongoDB Shell編)
[OCI]MongoDBクライアントからAutonomous JSON Databaseに接続してみた(mongoexport/mongoimport編)

我测试了一款以使用MongoDB为前提编写的应用程序,只需将连接目标更改为Autonomous JSON Database即可使其正常工作。

1.INSERT(insertOne()/insertMany())とSELECT(find())

検証のために、MongoDBに対するINSERT処理とSELECT処理を含むNode.jsのアプリケーションを作成しました。
(Node.js初心者なので、コードが美しくないのはご容赦ください)

const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
var async = require('async');

var url = "mongodb://admin:Demo%231Demo%231@127.0.0.1:27017/mongo?authSource=admin";
//var url = "mongodb://mongo:Demo%231Demo%231@XXXXXXXXXX-AJD1.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true";

console.log("Connected to  " + url + "\n");

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mongo");
  var coll = "dept";

  async.series([
// ----------------------------------------------------------------------
// Insert Single Row
// ----------------------------------------------------------------------
    function (callback) {
      var obj = {"deptno":10,"deptname":"ACCOUNTING","location":"NEW YORK"};
      dbo.collection(coll).insertOne(obj, function(err, res) {
        if (err) throw err;
        console.log("Inserted 1 document into collection " + coll + ".\n");
        callback(null, "first");
      });
    },
// ----------------------------------------------------------------------
// Select All Rows
// ----------------------------------------------------------------------
    function (callback) {
      dbo.collection(coll).find().toArray(function(err, result) {
        if (err) throw err;
        console.log("Select All documents in collection " + coll + ".\n");
        console.log(result);
        console.log();
        callback(null, "second");
      });
    },
// ----------------------------------------------------------------------
// INSERT Multiple Rows
// ----------------------------------------------------------------------
    function (callback) {
      var obj = [
        {"deptno":20,"deptname":"RESEARCH","location":"DALLAS"},
        {"deptno":30,"deptname":"SALES","location":"CHICAGO"},
        {"deptno":40,"deptname":"OPERATIONS","location":"BOSTON"}
      ];
      dbo.collection(coll).insertMany(obj, function(err, res) {
        if (err) throw err;
        console.log("Inserted 3 document into collection " + coll + ".\n");
        callback(null, "third");
      });
    },
// ----------------------------------------------------------------------
// Select All Rows
// ----------------------------------------------------------------------
    function (callback) {
      dbo.collection(coll).find().toArray(function(err, result) {
        if (err) throw err;
        console.log("Select All documents in collection " + coll + ".\n");
        console.log(result);
        console.log();
        callback(null, "fourth");
      });
    },
  ],
  function (err, results) {
    if (err) {
      throw err;
    }
    db.close();
    console.log('All done. ');
  });
});

首先,我們將連接到本地的MongoDB並嘗試執行。

[opc@work3 mongo]$ node insert.js 
Connected to  mongodb://admin:Demo%231Demo%231@127.0.0.1:27017/mongo?authSource=admin

Inserted 1 document into collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48b"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NEW YORK'
  }
]

Inserted 3 document into collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48b"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NEW YORK'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48c"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48d"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48e"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON'
  }
]

All done. 
[opc@work3 mongo]$ 

当然了,它成功地执行了。
接下来,我将把连接目标改为Autonomous JSON Database并尝试执行。

[opc@work3 mongo]$ node insert.js 
Connected to  mongodb://mongo:Demo%231Demo%231@XXXXXXXXXX-AJD1.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true

Inserted 1 document into collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba701a695c59e54bea6ed"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NEW YORK'
  }
]

Inserted 3 document into collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba701a695c59e54bea6ee"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6ef"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6f0"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6ed"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NEW YORK'
  }
]

All done. 
[opc@work3 mongo]$

这个MongoDB应用程序可以在Autonomous JSON Database上运行,只需更改连接地址,就可以正常执行INSERT和SELECT操作。

2. 更新(updateOne()/updateMany())

下一个是包含“更新”功能的应用程序。

const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
var async = require('async');

//var url = "mongodb://admin:Demo%231Demo%231@127.0.0.1:27017/mongo?authSource=admin";
var url = "mongodb://mongo:Demo%231Demo%231@XXXXXXXXXX-AJD1.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true";

console.log("Connected to  " + url + "\n");

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mongo"); //DB名
  var coll = "dept"; //コレクション名

  async.series([
// ----------------------------------------------------------------------
// Update Single Row
// ----------------------------------------------------------------------
    function (callback) {
      var where = {deptno: 10};
      var set = {$set: {location: "NY"}};
      dbo.collection(coll).updateOne(where, set, function(err, result) {
        if (err) throw err;
        console.log("Updated 1 document in collection " + coll + ".\n");
        callback(null, "first");
      });
    },
// ----------------------------------------------------------------------
// Select All Rows
// ----------------------------------------------------------------------
    function (callback) {
      dbo.collection(coll).find().toArray(function(err, result) {
        if (err) throw err;
        console.log("Select All documents in collection " + coll + ".\n");
        console.log(result);
        console.log();
        callback(null, "second");
      });
    },
// ----------------------------------------------------------------------
// Update All Rows
// ----------------------------------------------------------------------
    function (callback) {
      var where = {};
      var set = {$set: {country: "USA"}};
      dbo.collection(coll).updateMany(where, set, function(err, result) {
        if (err) throw err;
        console.log("Updated All documents in collection " + coll + ".\n");
        callback(null, "third");
      });
    },
// ----------------------------------------------------------------------
// Select All Rows
// ----------------------------------------------------------------------
    function (callback) {
      dbo.collection(coll).find().toArray(function(err, result) {
        if (err) throw err;
        console.log("Select All documents in collection " + coll + ".\n");
        console.log(result);
        console.log();
        callback(null, "fourth");
      });
    },
  ],
  function (err, results) {
    if (err) {
      throw err;
    }
    db.close();
    console.log('All done.' );
  });
});

首先,将连接目标设置为本地的MongoDB并尝试执行。

[opc@work3 mongo]$ node update.js 
Connected to  mongodb://admin:Demo%231Demo%231@127.0.0.1:27017/mongo?authSource=admin

Updated 1 document in collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48b"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NY'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48c"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48d"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48e"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON'
  }
]

Updated All documents in collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48b"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NY',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48c"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48d"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48e"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON',
    country: 'USA'
  }
]

All done.
[opc@work3 mongo]$ 

当然的,它被顺利执行了。
接下来,我将仅将连接目标更改为自治JSON数据库,并尝试执行。

[opc@work3 mongo]$ node update.js 
Connected to  mongodb://mongo:Demo%231Demo%231@XXXXXXXXXX-AJD1.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true

Updated 1 document in collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba701a695c59e54bea6ee"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6ef"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6f0"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6ed"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NY'
  }
]

Updated All documents in collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba701a695c59e54bea6ee"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6ef"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6f0"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6ed"),
    deptno: 10,
    deptname: 'ACCOUNTING',
    location: 'NY',
    country: 'USA'
  }
]

All done.
[opc@work3 mongo]$ 

经过只是更改连接的MongoDB应用程序可以确认在Autonomous JSON Database上可以无问题地运行更新操作。

最后是包含DELETE操作的应用程序。

const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
var async = require('async');

//var url = "mongodb://admin:Demo%231Demo%231@127.0.0.1:27017/mongo?authSource=admin";
var url = "mongodb://mongo:Demo%231Demo%231@SYA6VPHK3PZLKHQ-AJD1.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true";

console.log("Connected to  " + url + "\n");

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mongo"); //DB名
  var coll = "dept"; //コレクション名

  async.series([
// ----------------------------------------------------------------------
// Delete Single Row
// ----------------------------------------------------------------------
    function (callback) {
      var where = {deptno: 10};
      dbo.collection(coll).deleteOne(where, function(err, result) {
        if (err) throw err
        console.log("Deleted 1 document in collection " + coll + ".\n");
        callback(null, "first");
      });
    },
// ----------------------------------------------------------------------
// Select All Rows
// ----------------------------------------------------------------------
    function (callback) {
      dbo.collection(coll).find().toArray(function(err, result) {
        if (err) throw err;
        console.log("Select All documents in collection " + coll + ".\n");
        console.log(result);
        console.log();
        callback(null, "second");
      });
    },
// ----------------------------------------------------------------------
// Delete All Rows
// ----------------------------------------------------------------------
    function (callback) {
      var where = {};
      dbo.collection(coll).deleteMany(where, function(err, result) {
        if (err) throw err;
        console.log("Deleted All documents in collection " + coll + ".\n");
        callback(null, "third");
      });
    },
// ----------------------------------------------------------------------
// Select All Rows
// ----------------------------------------------------------------------
    function (callback) {
      dbo.collection(coll).find().toArray(function(err, result) {
        if (err) throw err;
        console.log("Select All documents in collection " + coll + ".\n");
        console.log(result);
        console.log();
        callback(null, "fourth");
      });
    },
  ],
  function (err, results) {
    if (err) {
        throw err;
    }
    db.close();
    console.log('All done.');
  });
});

首先,将连接目标设置为本地的MongoDB,并尝试执行。

[opc@work3 mongo]$ node delete.js 
Connected to  mongodb://admin:Demo%231Demo%231@127.0.0.1:27017/mongo?authSource=admin

Deleted 1 document in collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48c"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48d"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba8c870e8cfc3e3c3e48e"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON',
    country: 'USA'
  }
]

Deleted All documents in collection dept.

Select All documents in collection dept.

[]

All done.
[opc@work3 mongo]$ 

当然了,执行得非常顺利。
接下来,我将把连接目标更改为Autonomous JSON Database,然后再次进行执行。

[opc@work3 mongo]$ node delete.js 
Connected to  mongodb://mongo:Demo%231Demo%231@SYA6VPHK3PZLKHQ-AJD1.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true

Deleted 1 document in collection dept.

Select All documents in collection dept.

[
  {
    _id: new ObjectId("61eba701a695c59e54bea6ee"),
    deptno: 20,
    deptname: 'RESEARCH',
    location: 'DALLAS',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6ef"),
    deptno: 30,
    deptname: 'SALES',
    location: 'CHICAGO',
    country: 'USA'
  },
  {
    _id: new ObjectId("61eba701a695c59e54bea6f0"),
    deptno: 40,
    deptname: 'OPERATIONS',
    location: 'BOSTON',
    country: 'USA'
  }
]

Deleted All documents in collection dept.

Select All documents in collection dept.

[]

All done.
[opc@work3 mongo]$ 

我们确认了仅仅是将连接目标变更的MongoDB应用程序也可以在Autonomous JSON Database中正常执行DELETE操作。

总结

通过使用Oracle Database API for MongoDB,我们确认了Node.js应用程序可以连接到Autonomous JSON数据库并进行使用,只需更改连接目标即可

广告
将在 10 秒后关闭
bannerAds