Docs HomeMongoDB Manual

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描述
urlstringSpecifies 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,则还必须包含password参数。
passwordstringOptional unless the user parameter is specified. Specifies the password for the user.可选的,除非指定了user参数。指定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: 另请参阅: