翻译或纠错本页面

connect()

Description

connect(url, user, password)

Creates a connection to a MongoDB instance and returns the reference to the database. However, in most cases, use the Mongo() object and its getDB() method instead.

Parameter Type Description
url string

Specifies the connection string. You can specify either:

  • <hostname>:<port>/<database>
  • <hostname>/<database>
  • <database>
user string Optional. Specifies an existing username with access privileges for this database. If user is specified, you must include the password parameter as well.
password string Optional unless the user parameter is specified. Specifies the password for the user.

connect() 接收一个字符串 <hostname><:port>/<database> 参数用来连接到指定 <hostname><:port> 的实例并返回数据库 <database> 的引用地址。

下面的例子在实例化一个新的连接,连接到在当前主机上运行的 MongoDB 实例,关返回一个 myDatabase 数据库的引用:

db = connect("localhost:27017/myDatabase")
←   Mongo() Native Methods  →