バージョン
- node: v6.9.4
- npm: 3.10.10
- express: 4.14.0
- mongoDB: v3.4.3
- node-mongodb-native: v2.2
mongoDB入れる
$ brew install mongodb
# 起動確認
$ brew services start mongodb
$ brew services stop mongodb
レポジトリ作って、nodeのmongodb npmを入れる。
mongodb/node-mongodb-native
$ express -e mongodb-prot
$ cd mongodb-prot
$ npm install mongodb --save
本筋関係無いけど、ファイル変更でserver restartしてもらうためにnodemonを入れとく。
remy/nodemon
$ npm install -g nodemon
package.json
"scripts": {
"prestart": "eslint .", // eslint使う場合
"start": "node ./bin/www",
"dev": "nodemon ./bin/www"
},
な感じで書いて、
$ npm run dev
で動かす。(カスタムで入れたdevみたいな名前の場合はnpm dev
みたいに動かせなくて、npm run hoge
or npm run-script hoge
とする必要がある)
話題をmongodbに戻して、レポジトリ内にmongo用のフォルダを作っておく。
$ pwd /path/to/mongodb-prot
$ mkdir data
// mongodb起動
$ mongod --dbpath=data
ここからMongoDBで簡単なCRUDをやっていく。
app.js
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017/mongodb-prot';
// insert
function insertDocuments(db, callback) {
let collection = db.collection('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);
});
}
// findAll
function findAllDocuments(db, callback) {
let collection = db.collection('documents');
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log('Found the following records');
console.log(docs);
callback(docs);
});
}
// find
function findDocument(db, callback) {
let collection = db.collection('documents');
collection.find({'a': 3}).toArray(function(err, docs) {
assert.equal(err, null);
console.log('Found record');
console.log(docs);
callback(docs);
});
}
// update
function updateDocment(db, callback) {
const collection = db.collection('documents');
collection.updateOne({'a': 2},
{ $set: {'b': 1} }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log('Updated the document with the field a equal to 2');
callback(result);
});
}
// remove
function removeDocument(db, callback) {
const collection = db.collection('documents');
collection.deleteOne({'a': 3}, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log('Removed the document with the field a equal to 3');
callback(result);
});
}
// 接続して、上記のCRUDを試していく(適宜組み合わせて下さい)
// 下記はinsertしてfindAllしてる
MongoCient.connect(url, function(err, db) {
assert.equal(null, err);
console.log('connection successfully to server');
insertDocuments(db, function() {
findAllDocuments(db, function() {
db.close();
})
});
});
ドキュメントはこちら。
http://mongodb.github.io/node-mongodb-native/2.2/quick-start/quick-start/
あと、mongoのシェルも少しだけのぞいてみる。
# 起動
$ mongo
$ show dbs
$ use mongodb-prot
$ show collections
# ドキュメント数を確認(今回は'documents'と名付けたのでそれをチェック)
$ db.documents.count()
# findAll
$ db.documents.find({})
ドキュメントはこちら。
https://docs.mongodb.com/getting-started/shell/client/
今回いじったコードも置いておきます。
https://github.com/mazeltov7/mongodb-prot