connect()
On this page
This version of the documentation is archived and no longer supported. View the current documentation to learn how to upgrade your version of MongoDB server.
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 itsgetDB()
method instead.ParameterTypeDescriptionurl
stringSpecifies the connection string. You can specify either:
<hostname>:<port>/<database>
<hostname>/<database>
<database>
user
stringOptional. Specifies an existing username with access privileges for this database. Ifuser
is specified, you must include thepassword
parameter as well.password
stringOptional unless theuser
parameter is specified. Specifies the password for theuser
.
Example
The following example instantiates a new connection to the MongoDB
instance running on the localhost interface and returns a reference
to myDatabase
:
db = connect("localhost:27017/myDatabase")