connect()

On this page本页内容

Description描述

connect(url, user, password)

Creates a connection to a MongoDB instance and returns the reference to the database. 创建到MongoDB实例的连接,并返回对数据库的引用。However, in most cases, use the Mongo() object and its getDB() method instead.但是,在大多数情况下,使用Mongo()对象及其getDB()方法。

Parameter参数Type类型Description描述
urlstring

Specifies the connection string. 指定连接字符串。You can specify either:您可以指定:

  • <hostname>:<port>/<database>
  • <hostname>/<database>
  • <database>
userstringOptional. 可选。Specifies an existing username with access privileges for this database. 指定对此数据库具有访问权限的现有用户名。If user is specified, you must include the password parameter as well.如果指定了user,则还必须包括密码参数。
passwordstringOptional unless the user parameter is specified. 可选,除非指定了user参数。Specifies the password for the user.指定用户的密码。

Example示例

The following example instantiates a new connection to the MongoDB instance running on the localhost interface and returns a reference to myDatabase:以下示例实例化了一个到本地主机接口上运行的MongoDB实例的新连接,并返回对myDatabase的引用:

db = connect("localhost:27017/myDatabase")
Tip提示
See also: 参阅:
←  Connection MethodsMongo() →