connect()

Description

  • connect(url, user, password)
  • Creates a connection to a MongoDB instance and returns the referenceto the database. However, in most cases, use the Mongo()object and its getDB() method instead.

ParameterTypeDescriptionurlstringSpecifies 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.passwordstringOptional unless the user parameter is specified. Specifies thepassword for the user.

Example

The following example instantiates a new connection to the MongoDBinstance running on the localhost interface and returns a referenceto myDatabase:

  1. db = connect("localhost:27017/myDatabase")

See also

Mongo(), Mongo.getDB(), db.auth()