核心概念
| Oralce/MySQL | MongoDB | Mongoose |
|---|---|---|
| 表 | 集合 | 模板、模型 |
| 行 | 文档 | 实例 |
| 主键 | Object(_id) | Object(_id) |
| 表字段 | Field | Field |
mongoose官网 https://mongoosejs.com/
$ npm add mongoose --save -dev
编写 app.js
const mongoose = require('mongoose')// mongodb://账户名:密码@IP地址:端口号/要登录的数据库mongoose.connect('mongodb://test:123456@192.168.137.133:27017/testdb', {useNewUrlParser: true,useUnifiedTopology: true});// 创建模型const user = mongoose.model('users', { name: String, age: Number, email: String })// 输入数据const zws = new user({name: 'zws',age: 2,email: '95605355@qq.com'})// 保存数据,且返回的是这条数据的信息zws.save().then(res => {console.log('save Ok!', res)})
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.弃用警告:当前服务器发现和监视引擎已弃用,将在未来版本中删除。要使用新的服务器发现和监控引擎,将选项{useUnifiedTopology: true}传递给MongoClient构造函数。
安装 saslprep,密码安全:
$ npm install saslprep -S
