Ubuntu下MongoDB安装&使用NodeJS操作MongoDB
一、Ubuntu下安装MongoDB 原文
1, 导入公共key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA3129272, 为MongoDB创建资源列表
Ubuntu 12.04
echo "deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Ubuntu 14.04
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Ubuntu 16.04
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
3,更新本地系统
sudo apt-get update4,安装MongoDB
sudo apt-get install -y mongodb-org或者:sudo apt-get install mongodb-org -g --save
安装特定版本:
sudo apt-get install -y mongodb-org=3.2.10 mongodb-org-server=3.2.10 mongodb-org-shell=3.2.10 mongodb-org-mongos=3.2.10 mongodb-org-tools=3.2.105, 对Ubuntu 16.4,创建 systemd service
[Unit] Description=High-performance, schema-free document-oriented database After=network.target Documentation=https://docs.mongodb.org/manual [Service] User=mongodb Group=mongodb ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf [Install] WantedBy=multi-user.target6, 启动,停止,重启
sudo service mongod start
sudo service mongod stop
sudo service mongod restart二、使用NodeJS调用MongoDB 参考
1,使用insertMany向collection 'documents' 插入数据
<span style="font-size:14px;">var MongoClient = require('mongodb').MongoClient, assert = require('assert'); //Connection URL var url = 'mongodb://localhost:27017/data'; //Use connect method to connect to the server MongoClient.connect(url, function(err, db){ assert.equal(null, err); console.log("Connected successfully to server"); insertDocuments(db, function() { db.close(); }); }); var insertDocuments = function(db, callback) { // get the documents collection var 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); }); }</span>
2,使用remove()将两个collection清空
<span style="font-size:14px;">var MongoClient = require('mongodb').MongoClient, assert = require('assert'); //Connection URL var url = 'mongodb://localhost:27017/data'; //Use connect method to connect to the server MongoClient.connect(url, function(err, db){ assert.equal(null, err); console.log("Connected successfully to server"); removeDocuments(db, function() { db.close(); }); }); var removeDocuments = function(db, callback) { //get the documents collection var collection = db.collection('app-log-pm25-led'); //remove all documents in collection collection.remove({},function(err, result) { assert.equal(err, null); console.log("remove all the documents"); callback(result); }); //get the documents collection collection = db.collection('device-log-pm25'); //remove all documents in collection collection.remove({},function(err, result) { assert.equal(err, null); console.log("remove all the documents"); callback(result); }); }</span>
3,使用find()选取数据
<span style="font-size:14px;">var MongoClient = require('mongodb').MongoClient, assert = require('assert'); //Connection URL var url = 'mongodb://localhost:27017/data'; //Use connect method to connect to the server MongoClient.connect(url, function(err, db){ assert.equal(null, err); console.log("Connected successfully to server"); findDocuments(db, function() { db.close(); }); }); var findDocuments = function(db, callback) { //get the documents collection var collection = db.collection('app-log-pm25-led'); //find some documents collection.find({}).toArray(function(err, docs) { assert.equal(err, null); console.log("find the following records"); console.log(docs); callback(docs); }); }</span>
4,远程连接MongoDB并使用winston-mongodb远程保存日志
需先进行如下操作:
配置mongodb.conf 参考
- #bind_ip = 127.0.0.1 //注释此行
- auth = true //将此行前的注释去掉
<pre name="code" class="javascript"><span style="font-size:14px;">var winston = require('winston'); //winston logger require('winston-mongodb').MongoDB; var options = { db:'mongodb://10.167.35.105:27017/data', collection:'documents', username:'mongodb' } winston.add(winston.transports.MongoDB, options); winston.info('{a:10}');</span>