mongoose连接数据库(connect函数)

数据库的连接

const mongoose = require('mongoose')
// 如果数据库(database)不存在就会创建
mongoose.connect('mongodb://localhost/eg', 
  { useNewUrlParser: true, useUnifiedTopology: true },
  function (err) {
    if (err)
      console.log('连接数据库失败');
    else {
      console.log('连接数据库成功');
    }
  })
  • mongoose.connect函数
function connect(
  uris: string, 
  options: mongoose.ConnectOptions, 
  callback: (err: MongoError) => void
)
//Opens the default mongoose connection. 
  1. 函数返回promise
  2. uris:'mongoose://username:password@ipadress:port/databaseName'
  3. options:其中的设置可以覆盖uris中的设置
posted @ 2021-05-23 19:30  丁同亚的博客  阅读(1997)  评论(0编辑  收藏  举报