【问题标题】:What does useNewURLParser and userCreateIndex in mongoose.connect() do?mongoose.connect() 中的 useNewURLParser 和 useCreateIndex 有什么作用?
【发布时间】:2021-03-17 09:07:24
【问题描述】:

我目前是 MongoDB 和 NodeJS 的新手,我想知道选项 useNewURLParser 和 useCreateIndex 的作用。

const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1:27017/task-manager-api",
{
    useNewUrlParser: true,
    useCreateIndex: true
})

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

MongoDB 有两种连接字符串格式。 旧格式现已弃用,并使用旧 URL 格式。有mongodb+srv:// URL 和简单的mongodb:// URL。如果您使用的是新格式(您可能默认使用),新的 URL 解析器将不再支持旧样式的 url。

useCreateIndex:之前 MongoDB 再次使用ensureIndex 函数调用来确保索引存在,如果不存在,则创建一个。 这也因createIndex 而被弃用。 useCreateIndex 选项确保您正在使用新的函数调用。

参考:

https://mongoosejs.com/docs/connections.html#options https://mongoosejs.com/docs/deprecations.html

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-09-14
  • 2018-09-15
  • 2019-10-11
  • 2019-06-27
  • 1970-01-01
  • 2022-01-05
  • 2012-07-07
相关资源
最近更新 更多