MongoDB

MongoDB 安装

https://www.mongodb.com/download-center/community 这是 MongoDB 的下载中心,选择需要下载的版本和操作系统。注意,MongoDB 的稳定版本都是偶数的。

Mac 系统下安装

Mac 系统可通过 Homebrew 安装 MongoDB 还是相对较简单的

  1. $ brew update # 更新 Homebrew 数据包
  2. $ brew install mongodb # 安装 MongoDB

Linux 系统安装

  1. $ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.2.tgz
  2. $ tar -xvf mongodb-linux-x86_64-ubuntu1604-4.2.2.tgz
  3. $ mv mongodb-linux-x86_64-ubuntu1604-4.2.2 mongodb-4.2.2
  4. $ export PATH=$PATH:/data/soft/mongodb-4.2.2/bin

安装校验

查看 MongoDB 版本,校验是否安装成功,出现以下信息则安装成功

  1. $ mongo --version
  2. MongoDB shell version v4.2.2

开启 MongoDB 实例

启动 MongoDB

通过 --dbpath 参数自定义目录路径,若省略 --dbpath 参数,将会使用系统默认的数据目录 (/data/db)

  1. $ mongod --dbpath <自定义的目录路径>

检查是否成功启动

出现以下信息链接成功,默认端口为 27017

  1. 2018-01-28T21:20:50.190+0800 I CONTROL [initandlisten] MongoDB starting : pid=10488 port=27017 dbpath=/Users/may/documents/mongodb/data 64-bit host=XXXX.local
  2. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] db version v3.6.22018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
  3. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
  4. 2018-01-28T21:20:50.979+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/Users/may/documents/mongodb/data/diagnostic.data'
  5. 2018-01-28T21:20:50.980+0800 I NETWORK [initandlisten] waiting for connections on port 27017

打开 mongodb 客户端

打开本地控制台输入命令 mongo 连接到 mongodb 服务器

  1. $ mongo

看到以下信息链接成功,会显示 mongodb 的版本号、链接地址,默认端口号为 27017

  1. MongoDB shell version v3.6.2
  2. connecting to: mongodb://127.0.0.1:27017
  3. MongoDB server version: 3.6.2
  4. Server has startup warnings:
  5. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  6. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
  7. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
  8. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  9. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
  10. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
  11. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
  12. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
  13. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
  14. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
  15. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  16. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  17. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

展示所有数据库

  1. > show dbs
  2. admin 0.000GB
  3. config 0.000GB
  4. local 0.000GB

查看当前所连接的数据库

  1. > db
  2. test

查看当前数据库下的所有集合(数据表)

  1. > show collections
  2. demo_admin

切换/创建数据库

切换数据库,使用use之前我们不需要对数据库进行额外的创建,在mongo中会在需要的时候自己创建。

  1. > use demo
  2. switched to db demo

Nodejs 客户端链接 MongoDB

  1. const MongoClient = require('mongodb').MongoClient;
  2. const mongodbUrl = "mongodb://localhost:27017/demo";
  3. const DB = {};
  4. const userCollName = 'user'; //指定集合名
  5. //封装初始化数据库方法
  6. const init = () => {
  7. if(DB.db){
  8. return Promise.reject('mongodb has already initialized');
  9. }
  10. return MongoClient.connect(mongodbUrl)
  11. .then(db => {
  12. DB.db = db;
  13. DB.userColl = db.collection(userCollName);
  14. //退出关闭mongodb数据库连接
  15. process.on('exit', () => db.close());
  16. console.log('mongodb initialized');
  17. });
  18. }

问题汇总

  1. db.collection is not a function

是因为 mongodb 版本是 3.0 以上版本,将 mongodb 版本指定安装为 2.2.34 即可解决,npm 安装

  1. npm install mongodb@^2.2.34 --save