核心概念

Oralce/MySQL MongoDB Mongoose
表 集合 模板、模型
行 文档 实例
主键 Object(_id) Object(_id)
表字段 Field Field

mongoose官网 https://mongoosejs.com/

  1. $ npm add mongoose --save -dev

编写 app.js

  1. const mongoose = require('mongoose')
  2. // mongodb://账户名:密码@IP地址:端口号/要登录的数据库
  3. mongoose.connect('mongodb://test:123456@192.168.137.133:27017/testdb', {
  4. useNewUrlParser: true,
  5. useUnifiedTopology: true
  6. });
  7. // 创建模型
  8. const user = mongoose.model('users', { name: String, age: Number, email: String })
  9. // 输入数据
  10. const zws = new user({
  11. name: 'zws',
  12. age: 2,
  13. email: '95605355@qq.com'
  14. })
  15. // 保存数据,且返回的是这条数据的信息
  16. zws.save().then(res => {
  17. console.log('save Ok!', res)
  18. })
  1. DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
  2. 弃用警告:当前服务器发现和监视引擎已弃用,将在未来版本中删除。要使用新的服务器发现和监控引擎,将选项{useUnifiedTopology: true}传递给MongoClient构造函数。

安装 saslprep,密码安全:

  1. $ npm install saslprep -S