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 itsgetDB()
method instead.Mongo()
对象及其getDB()
方法。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 thepassword
parameter as well.user
,则还必须包含password
参数。password
string Optional unless the可选的,除非指定了user
parameter is specified. Specifies the password for theuser
.user
参数。指定user
的密码。
Example实例
The following example instantiates a new connection to the MongoDB instance running on the localhost interface and returns a reference to 以下示例实例化了与本地主机接口上运行的MongoDB实例的新连接,并返回对myDatabase
:myDatabase
的引用:
db = connect("localhost:27017/myDatabase")
Tip