mongoose4.5中文教程
  • Introduction
  • Mongoose
  • guide
    • 模式(schemas)
      • 模式类型(types)
      • 自定义类型(custom)
    • 模型(models)
    • 文档(documents)
      • 子文档(sub docs)
      • 默认值(defaults)
    • 查询(queries)
    • 验证(validation)
    • 中间件(middleware)
    • 联表(population)
    • 连接(connections)
    • 插件(plugins)
    • 承诺(promises)
    • 鉴频器(discriminators)
    • 贡献
    • ES2015 整合
    • 浏览器中的schemas
    • 自定义schema类型
    • MongoDB版本兼容性
    • 3.6 发布说明
    • 3.8 发布说明
    • 4.0 发布说明
  • API 文档
Powered by GitBook
On this page
  • Connections
  • 选项
  • 连接字符串选项
  • 关于keepAlive
  • 复制集连接
  • 多mongos的支持
  • 多个连接
  • 连接池
  • 下一步

Was this helpful?

  1. guide

连接(connections)

Previous联表(population)Next插件(plugins)

Last updated 6 years ago

Was this helpful?

原文: 翻译:小虾米(QQ:509129)

Connections

我们可以通过利用mongoose.connect()方法连接MongoDB 。

mongoose.connect('mongodb://localhost/myapp');

这是最需要的在连接 myapp数据库运行在默认端口(27017)上。如果本地连接失败,然后尝试用127.0.0.1不是localhost。有时可能会出现问题,当本地主机名已更改。

我们还可以视你的环境而定在URI指定几个参数:

mongoose.connect('mongodb://username:password@host:port/database?options...');

查看 了解更多。

选项

该连接方法还接受一个选项对象,该对象将被传递给底层驱动程序。这里包含的所有选项优先于连接字符串中传递的选项。

mongoose.connect(uri, options);

以下是可用的选项键:

  • db - passed to the

  • server - passed to the

  • replset - passed to the

  • user - username for authentication (if not specified in uri)

  • pass - password for authentication (if not specified in uri)

  • auth - options for authentication

  • mongos - passed to the

  • promiseLibrary - sets the

例子:

var options = {
  db: { native_parser: true },
  server: { poolSize: 5 },
  replset: { rs_name: 'myReplicaSetName' },
  user: 'myUserName',
  pass: 'myPassword'
}
mongoose.connect(uri, options);

注意: 服务器选项auto_reconnect默认为真的可以重写。数据库选项forceserverobjectid设置为false将不能被重写。

// Good way to make sure mongoose never stops trying to reconnect
mongoose.connect(uri, { server: { reconnectTries: Number.MAX_VALUE } });

连接字符串选项

mongoose支持以下的连接字符串选项。

关于keepAlive

对于长时间运行的应用程序,它往往是谨慎开启keepAlive数毫秒。没有它,在一段时间后,你可能会开始看到“连接关闭”的错误,似乎没有理由。如果是这样的话,读了这些之后,你可能会决定启用KeepAlive:

options.server.socketOptions = options.replset.socketOptions = { keepAlive: 120 };
mongoose.connect(uri, options);

复制集连接

用同样方法连接到一个复制集但是通过逗号分隔uris的列表。

mongoose.connect('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]' [, options]);

多mongos的支持

高可用性在多mongoss情况下也支持。通过你的mongos实例的连接字符串和设置mongos选项为true:

mongoose.connect('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);

多个连接

到目前为止,我们已经看到了如何连接到使用Mongoose默认连接MongoDB。有时我们可能需要多个连接到Mongo,各有不同的读/写设置,或者只是不同的数据库为例。在这些情况下,我们可以利用mongoose.createConnection()接受所有已经讨论的争论并返回你一个新的连接。

var conn = mongoose.createConnection('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]', options);

此连接对象,然后用于创建和检索模型。模型总是局限于单个连接。

连接池

每个连接,使用mongoose.connect或mongoose.createconnection创造所有的内部配置连接池的默认大小为5的支持 。使用您的连接选项调整池大小:

// single server
var uri = 'mongodb://localhost/test';
mongoose.createConnection(uri, { server: { poolSize: 4 }});

// for a replica set
mongoose.createConnection(uri, { replset: { poolSize: 4 }});

// passing the option in the URI works with single or replica sets
var uri = 'mongodb://localhost/test?poolSize=4';
mongoose.createConnection(uri);

下一步

有关可用选项的更多信息,见。

注意:如果auto_reconnect设置成on,mongoose会放弃试图恢复一定数量的失败后。设置增加mongoose尝试重新连接的次数。

现在我们已经掌握了connections,让我们看看我们如何能将我们的功能的碎片变成可重用的并共享。

Connections
mongodb connection string spec
underlying driver's db instance
underlying driver's server instance(s)
underlying driver's ReplSet instance
underlying driver's mongos options
underlying driver's promise library
driver
server.reconnectTries和server.reconnectInterval options选项
ssl
poolSize
autoReconnect
socketTimeoutMS
connectTimeoutMS
authSource
retries
reconnectWait
rs_name
replicaSet
nativeParser
w
journal
wtimeoutMS
readPreference
readPreferenceTags
sslValidate
插件