【Node】Node.js ~ 基本編 / MongoDB を使う ~

 ■ 初期設定

1)MongoDB をインストールする
2)Node.jsの MongoDB ライブラリをインストールする

1)MongoDBをインストールする

を参考に MongoDB をインストールする

2)Node.jsのMongoDBライブラリをインストールする
https://dk521123.hatenablog.com/entry/2018/06/13/234315

で行った npm でインストールする 
npm install mongodb

■ サンプル

settings.js

exports.port='18080';
exports.host='127.0.0.1';
exports.db='nodedb';

hellonode.js

// MongoDBジュールを呼び出す
var mongoDb = require('mongodb');
var mongoClient = mongoDb.MongoClient;

// 外部ファイルから取得
var settings = require('./settings.js');

mongoClient.connect("mongodb://" + settings.host + "/" + settings.db,
function(error, client) {
  if (error) {
    return console.dir(error);
  }

  console.log("Connected to DB...");

  var db = client.db(settings.db);
  db.collection("users", function(error, collection) {
    if (error) {
      return console.dir(error);
     }

    var dataset = [
       {id: 1, name: "Mike", score: 64},
       {id: 2, name: "Tom", score: 98},
       {id: 3, name: "Smith", score: 25},
       {id: 4, name: "Kevin", score: 67}
    ];

    // INSERT
    collection.insert(dataset, function(error, result) {
       console.dir(result);
    });

    // SELECT - 1
    collection.find().toArray(function(err, items) {
        console.log(items);
     });

    // SELECT1 - '
    collection.find({name: "Mike"}).toArray(function(err, items) {
        console.log(items);
     });

    // SELECT - 2
    var stream = collection.find().stream();
    stream.on("data", function(items) {
       console.log(items);
     });

    stream.on("end", function() {
       console.log("Done");
     });
  });
});

動作確認

実行結果
$ node hellomongo.js

Connected to DB... { result: { ok: 1, n: 4 }, ops: [ { id: 1, name: 'Mike', score: 64, id: [Object] }, { id: 2, name: 'Tom', score: 98, id: [Object] }, { id: 3, name: 'Smith', score: 25, id: [Object] }, { id: 4, name: 'Kevin', score: 67, id: [Object] } ], insertedCount: 4, insertedIds: { '0': ObjectID { bsontype: 'ObjectID', id: [Object] }, '1': ObjectID { bsontype: 'ObjectID', id: [Object] }, '2': ObjectID { bsontype: 'ObjectID', id: [Object] }, '3': ObjectID { bsontype: 'ObjectID', id: [Object] } } } [ { id: 5b25d31535efee3a429de75b, id: 1, name: 'Mike', score: 64 }, { id: 5b25d31535efee3a429de75c, id: 2, name: 'Tom', score: 98 }, { id: 5b25d31535efee3a429de75d, id: 3, name: 'Smith', score: 25 }, { id: 5b25d31535efee3a429de75e, id: 4, name: 'Kevin', score: 67 } ] [ { id: 5b25d31535efee3a429de75b, id: 1, name: 'Mike', score: 64 } ] { id: 5b25d31535efee3a429de75b, id: 1, name: 'Mike', score: 64 } { id: 5b25d31535efee3a429de75c, id: 2, name: 'Tom', score: 98 } { id: 5b25d31535efee3a429de75d, id: 3, name: 'Smith', score: 25 } { _id: 5b25d31535efee3a429de75e, id: 4, name: 'Kevin', score: 67 } Done

** MongoDB内を確認する **  

mongo

MongoDB shell version v3.6.5 ・・・略・・・

show dbs ・・・略・・・ nodedb 0.000GB

use nodedb switched to db nodedb

show collections users

db.users.find(); { "id" : ObjectId("5b25d31535efee3a429de75b"), "id" : 1, "name" : "Mike", "score" : 64 } { "id" : ObjectId("5b25d31535efee3a429de75c"), "id" : 2, "name" : "Tom", "score" : 98 } { "id" : ObjectId("5b25d31535efee3a429de75d"), "id" : 3, "name" : "Smith", "score" : 25 } { "id" : ObjectId("5b25d31535efee3a429de75e"), "id" : 4, "name" : "Kevin", "score" : 67 }

# 参考文献
https://qiita.com/mimizq/items/76d3a948acb33881c8db  

# 関連記事
** Node.js ~ 基礎知識 / 環境構築編 ~ **  
https://dk521123.hatenablog.com/entry/2018/06/05/211900