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描述urlstring Specifies the connection string. You can specify either:指定连接字符串。您可以指定以下任一项:<hostname>:<port>/<database><hostname>/<database><database>
userstring Optional.可选的。Specifies an existing username with access privileges for this database.指定具有此数据库访问权限的现有用户名。If如果指定了useris specified, you must include thepasswordparameter as well.user,则还必须包含password参数。passwordstring Optional unless the可选的,除非指定了userparameter 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