1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[OCI]MongoDBクライアントからAutonomous JSON Databaseに接続してみた(Node.jsアプリケーション編)

Last updated at Posted at 2022-01-22

はじめに

これまでの以下の記事で、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初心者なので、コードが美しくないのはご容赦ください)

insert.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]$

こちらも問題なく実行できました
INSERT処理、SELECT処理をおこなうMongoDBのアプリケーションが、接続先を変更しただけで、そのままAutonomous JSON Databaseで実行できることが確認できました。

2.UPDATE(updateOne()/UpdateMany())

次はUPDATE処理を含むアプリケーションです。

update.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"); //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]$ 

当然ですが、問題なく実行されました。
次に、接続先のみをAutonomous JSON Databaseに変えて実行してみます。

[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]$ 

こちらも問題なく実行できました
UPDATE処理をおこなうMongoDBのアプリケーションが、接続先を変更しただけで、そのままAutonomous JSON Databaseで実行できることが確認できました。

#3 .DELETE(deleteOne()/deleteMany())
最後はDELETE処理を含むアプリケーションです。

delete.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@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]$ 

こちらも問題なく実行できました
DELETE処理をおこなうMongoDBのアプリケーションが、接続先を変更しただけで、そのままAutonomous JSON Databaseで実行できることが確認できました。

まとめ

Oracle Database API for MongoDBを利用することで、MongoDBを使用するNode.jsアプリケーションが、接続先を変更するだけでAutonomous JSON Databaseに接続して利用できることが確認できました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?