new MongoClient(){MongoClient}
Creates a new MongoClient instance
Returns:
MongoClient instance.Methods
-
staticMongoClient.connect(url, options, callback){Promise}
-
Connect to MongoDB using a url as documented at
docs.mongodb.org/manual/reference/connection-string/
Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
Name Type Description urlstring The connection URI string
optionsobject optional Optional settings.
Name Type Default Description poolSizenumber 5 optional poolSize The maximum size of the individual server pool.
sslboolean false optional Enable SSL connection.
sslCABuffer optional SSL Certificate store binary buffer
sslCRLBuffer optional SSL Certificate revocation list binary buffer
sslCertBuffer optional SSL Certificate binary buffer
sslKeyBuffer optional SSL Key file binary buffer
sslPassstring optional SSL Certificate pass phrase
checkServerIdentityboolean | function true optional Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
autoReconnectboolean true optional Enable autoReconnect for single server instances
noDelayboolean true optional TCP Connection no delay
familynumber 4 optional Version of IP stack. Defaults to 4.
keepAliveboolean 30000 optional The number of milliseconds to wait before initiating keepAlive on the TCP socket.
connectTimeoutMSnumber 30000 optional TCP Connection timeout setting
socketTimeoutMSnumber 360000 optional TCP Socket timeout setting
reconnectTriesnumber 30 optional Server attempt to reconnect #times
reconnectIntervalnumber 1000 optional Server will wait # milliseconds between retries
haboolean true optional Control if high availability monitoring runs for Replicaset or Mongos proxies.
haIntervalnumber 10000 optional The High availability period for replicaset inquiry
replicaSetstring optional The Replicaset set name
secondaryAcceptableLatencyMSnumber 15 optional Cutoff latency point in MS for Replicaset member selection
acceptableLatencyMSnumber 15 optional Cutoff latency point in MS for Mongos proxies selection.
connectWithNoPrimaryboolean false optional Sets if the driver should connect even if no primary is available
authSourcestring optional Define the database to authenticate against
auth.userstring optional The username for auth
auth.passwordstring optional The password for auth
wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
forceServerObjectIdboolean false optional Force server to assign _id values instead of driver.
serializeFunctionsboolean false optional Serialize functions on any object.
ignoreUndefinedBoolean false optional Specify if the BSON serializer should ignore undefined fields.
rawboolean false optional Return document results as raw BSON buffers.
promoteLongsboolean true optional Promotes Long values to number if they fit inside the 53 bits resolution.
promoteBuffersboolean false optional Promotes Binary BSON values to native Node Buffers.
promoteValuesboolean true optional Promotes BSON values to native types where possible, set to false to only receive wrapper types.
bufferMaxEntriesnumber -1 optional Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
domainsEnabledboolean false optional Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
pkFactoryobject null optional A primary key factory object for generation of custom _id keys.
promiseLibraryobject null optional A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
readConcernobject null optional Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
Name Type Default Description levelstring 'local' optional Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported)
maxStalenessSecondsnumber optional The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
appnamestring optional The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections.
loggerLevelstring optional The logging level (error/warn/info/debug)
loggerobject optional Custom logger object
validateOptionsobject false optional Validate MongoClient passed in options for correctness.
callbackMongoClient~connectCallback optional The command result callback
Returns:
Promise if no callback passed
-
connect(url, options, callback){Promise}
-
Connect to MongoDB using a url as documented at
docs.mongodb.org/manual/reference/connection-string/
Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
Name Type Description urlstring The connection URI string
optionsobject optional Optional settings.
Name Type Default Description poolSizenumber 5 optional poolSize The maximum size of the individual server pool.
sslboolean false optional Enable SSL connection.
sslCABuffer optional SSL Certificate store binary buffer
sslCRLBuffer optional SSL Certificate revocation list binary buffer
sslCertBuffer optional SSL Certificate binary buffer
sslKeyBuffer optional SSL Key file binary buffer
sslPassstring optional SSL Certificate pass phrase
checkServerIdentityboolean | function true optional Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
autoReconnectboolean true optional Enable autoReconnect for single server instances
noDelayboolean true optional TCP Connection no delay
familynumber 4 optional Version of IP stack. Defaults to 4.
keepAlivenumber 30000 optional The number of milliseconds to wait before initiating keepAlive on the TCP socket.
connectTimeoutMSnumber 30000 optional TCP Connection timeout setting
socketTimeoutMSnumber 360000 optional TCP Socket timeout setting
reconnectTriesnumber 30 optional Server attempt to reconnect #times
reconnectIntervalnumber 1000 optional Server will wait # milliseconds between retries
haboolean true optional Control if high availability monitoring runs for Replicaset or Mongos proxies.
haIntervalnumber 10000 optional The High availability period for replicaset inquiry
replicaSetstring optional The Replicaset set name
secondaryAcceptableLatencyMSnumber 15 optional Cutoff latency point in MS for Replicaset member selection
acceptableLatencyMSnumber 15 optional Cutoff latency point in MS for Mongos proxies selection.
connectWithNoPrimaryboolean false optional Sets if the driver should connect even if no primary is available
authSourcestring optional Define the database to authenticate against
auth.userstring optional The username for auth
auth.passwordstring optional The password for auth
wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
forceServerObjectIdboolean false optional Force server to assign _id values instead of driver.
serializeFunctionsboolean false optional Serialize functions on any object.
ignoreUndefinedBoolean false optional Specify if the BSON serializer should ignore undefined fields.
rawboolean false optional Return document results as raw BSON buffers.
promoteLongsboolean true optional Promotes Long values to number if they fit inside the 53 bits resolution.
promoteBuffersboolean false optional Promotes Binary BSON values to native Node Buffers.
promoteValuesboolean true optional Promotes BSON values to native types where possible, set to false to only receive wrapper types.
bufferMaxEntriesnumber -1 optional Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
domainsEnabledboolean false optional Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
pkFactoryobject null optional A primary key factory object for generation of custom _id keys.
promiseLibraryobject null optional A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
readConcernobject null optional Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
Name Type Default Description levelstring 'local' optional Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported)
maxStalenessSecondsnumber optional The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
appnamestring optional The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections.
loggerLevelstring optional The logging level (error/warn/info/debug)
loggerobject optional Custom logger object
validateOptionsobject false optional Validate MongoClient passed in options for correctness.
callbackMongoClient~connectCallback optional The command result callback
Returns:
Promise if no callback passed
Type Definitions
-
connectCallback(error, db)
-
The callback format for results
Name Type Description errorMongoError An error instance representing the error during the execution.
dbDb The connected database.