zoukankan      html  css  js  c++  java
  • Ubuntu下MongoDB安装&使用NodeJS操作MongoDB

    一、Ubuntu下安装MongoDB 原文

    1, 导入公共key

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
    2, 为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 update
    4,安装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.10
    5, 对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.target
    6, 启动,停止,重启

    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 参考

    1. #bind_ip = 127.0.0.1     //注释此行  
    2. 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>

    
    








  • 相关阅读:
    Git 历史/术语/命令/基本操作
    SQL 术语/语法/基本操作-必知必会
    bootstrap cdn地址
    IDEA 快捷键 大幅提高工作效率
    Django3 模版配置/过滤器/markdown=9
    Django3 路由文件位置/文件格式/路由传值=8
    Django3 创建项目/app全流程=7
    VS Code Django解决不必要报错
    Django3 如何使用静态文件/如何自定义后台管理页面=6
    Django3 如何编写单元测试和全面测试=5
  • 原文地址:https://www.cnblogs.com/sunhaoli/p/6764933.html
Copyright © 2011-2022 走看看