IMUZA.com

Xserver<<WordPress(ConoHa)<<はてなブログ

ホーム / Node.js / Node.js から MongoDB への接続テスト

Node.js から MongoDB への接続テスト

2018/01/18 Node.js, Windows

Node.js から MongoDB への接続テスト

関連記事

結局、前記事(上記関連記事)の「The file system cache of this machine is configured to be greater than 40% of the total memory.」はまだ消えていません。とりあえず、Node.js からデータベースに接続してみようと思います。

  • Node.js に MongoDB ドライバーを入れる
  • MongoDB に接続
  • ドキュメントの挿入
  • ドキュメントの取得

Node.js に MongoDB ドライバーを入れる

まず、MongoDB を起動しておきます。ただ、まだ認証できるユーザを作っていませんので、WARNING を出したままで進みます。

>mongod --dbpath c:\mongodb\data

もうひとつコマンドプロンプトを立ち上げ、

適当なフォルダをつくり、移動して npm init で package.json をつくり、mongodb のドライバーモジュールをインストールします。

MongoDB に接続

以下のチェック用スクリプトは、Quick Start にあります。

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');


// Connection URL
const url = 'mongodb://localhost:27017';


// Database Name
const dbName = 'myproject';


// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");


  const db = client.db(dbName);


  client.close();
});

接続されています。

ドキュメントの挿入

const insertDocuments = function(db, callback) {
  // Get the documents collection
  const collection = db.collection('documents');
  // Insert some documents
  collection.insertMany([
    {a : 1}, {a : 2}, {a : 3}
  ], function(err, result) {
    assert.equal(err, null);
    assert.equal(3, result.result.n);
    assert.equal(3, result.ops.length);
    console.log("Inserted 3 documents into the collection");
    callback(result);
  });
}


const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');


// Connection URL
const url = 'mongodb://localhost:27017';


// Database Name
const dbName = 'myproject';


// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");


  const db = client.db(dbName);


  insertDocuments(db, function() {
    client.close();
  });
});

3つのドキュメントを挿入する関数 insertDocument を書き加え呼び出します。

ドキュメントの取得

const findDocuments = function(db, callback) {
  // Get the documents collection
  const collection = db.collection('documents');
  // Find some documents
  collection.find({}).toArray(function(err, docs) {
    assert.equal(err, null);
    console.log("Found the following records");
    console.log(docs)
    callback(docs);
  });
}

全てのドキュメントを取得する関数を書き加えます。

  findDocuments(db, function() {
    client.close();
});

呼び出しのメソッドを書き換えます。

挿入を2回繰り返していますの6個のドキュメントが挿入されています。

といった感じで、リンク先の Quick Start にはドキュメントの選択取得、更新、削除、インデックス化のサンプルコードがあります。

MongoDB 起動後の WARNING を消す
アクセス制御を有効にした MongoDB に接続する
Twitter
Facebook
ブックマーク
LINEで送る

最初のサイドバー

最新記事

2023/01/26

WordPress:メニューのid,classを整理カスタマイズ

2023/01/19

WordPress:JSON-LD構造化データをプラグインなしで出力

2023/01/11

WordPress:OGPタグをプラグインなしで挿入する

2022/12/27

WordPress:canonicalタグをプラグインなしで制御する

2022/12/21

WordPress:robotsメタタグをプラグインなしで制御する

最新記事を一覧で見る

よく読まれている記事

よく読まれている記事を一覧で見る

カテゴリー

  • はてなブログ214
  • WebTips108
  • javascript98
  • Joomla!88
  • Windows68
  • CSS63
  • Wordpress59
  • Joomla!更新53
  • Linux49
  • はてなテーマ45
  • Plamo33
  • Google32
  • はてなプラグイン25
  • php22
  • Node.js18
  • Ubuntu16
  • SASS16
  • laravel415
  • Chrome11
  • cms-style10
  • iPhone9
  • genesis6
  • ConoHa WING6
  • Git入門6
  • Python5
  • Android5
  • スマートフォン4
  • PC全般4
  • 静的サイトジェネレーター3
  • Firefox3
  • SSD3
  • Facebook3
  • Blankslate3
  • Docker3
  • Mactype2
  • GitHub2
  • youtube1
  • rails入門1
  • Twitter1
  • はてなブクマ1
  • 映画1
  • Xserver1

Footer

My Web Sites

  • @半径とことこ60分
  • そんなには褒めないよ。映画評
  • IMUZA.com
  • GitHub

Related Sites

  • WordPress公式
  • WordPress関数リファレンス
  • PHPマニュアル

Contact Us

  • お問い合わせフォーム
  • Twitter
  • Facebook
  • Feedly

Copyright © 2023 · IMUZA.com