new MongoClient(url, options)
Creates a new MongoClient instance
| Name | Type | Description |
|---|---|---|
url |
string |
The connection URI string |
options |
MongoClientOptions |
optional
Optional settings |
Extends
- EventEmitter
Methods
-
staticMongoClient.connect(url, options){Promise.<MongoClient>}
-
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
optionsMongoClientOptions optional Optional settings
Returns:
Promise if no callback passed
-
close(force, callback){Promise}
-
Close the db and its underlying connections
Name Type Default Description forceboolean false optional Force close, emitting no events
callbackDb~noResultCallback optional The result callback
Returns:
Promise if no callback passed
-
connect(callback){Promise.<MongoClient>}
-
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 callbackMongoClient~connectCallback optional The command result callback
Returns:
Promise if no callback passed
-
db(dbName, options){Db}
-
Create a new Db instance sharing the current socket connections. Be aware that the new db instances are
related in a parent-child relationship to the original instance so that events are correctly emitted on child
db instances. Child db instances are cached so performing db('db1') twice will return the same instance.
You can control these behaviors with the options noListener and returnNonCachedInstance.Name Type Description dbNamestring optional The name of the database we want to use. If not provided, use database name from connection string.
optionsobject optional Optional settings.
Name Type Default Description noListenerboolean false optional Do not make the db an event listener to the original connection.
returnNonCachedInstanceboolean false optional Control if you want to return a cached instance or have a new one created
-
isConnected(options){boolean}
-
Check if MongoClient is connected
Name Type Description optionsobject optional Optional settings.
Name Type Default Description noListenerboolean false optional Do not make the db an event listener to the original connection.
returnNonCachedInstanceboolean false optional Control if you want to return a cached instance or have a new one created
- Deprecated
- Yes
-
startSession(options){ClientSession}
-
Starts a new session on the server
Name Type Description optionsSessionOptions optional optional settings for a driver session
Returns:
newly established session
-
watch(pipeline, options){ChangeStream}
-
Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this cluster. Will ignore all changes to system collections, as well as the local, admin,
and config databases.Name Type Description pipelineArray optional An array of aggregation pipeline stages through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
optionsobject optional Optional settings
Name Type Default Description fullDocumentstring 'default' optional Allowed values: ‘default’, ‘updateLookup’. When set to ‘updateLookup’, the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.
resumeAfterobject optional Specifies the logical starting point for the new change stream. This should be the _id field from a previously returned change stream document.
maxAwaitTimeMSnumber optional The maximum amount of time for the server to wait on new documents to satisfy a change stream query
batchSizenumber 1000 optional The number of documents to return per batch. See aggregation documentation.
collationobject optional Specify collation settings for operation. See aggregation documentation.
readPreferenceReadPreference optional The read preference. See read preference documentation.
startAtOperationTimeTimestamp optional receive change events that occur after the specified timestamp
sessionClientSession optional optional session to use for this operation
- Since:
- 3.1.0
Returns:
ChangeStream instance.
-
withSession(options, operation){Promise}
-
Runs a given operation with an implicitly created session. The lifetime of the session
will be handled without the need for user interaction.NOTE: presently the operation MUST return a Promise (either explicit or implicity as an async function)
Name Type Description optionsObject optional Optional settings to be appled to implicitly created session
operationfunction An operation to execute with an implicitly created session. The signature of this MUST be
(session) => {}
Type Definitions
-
connectCallback(error, client)
-
The callback format for results
Name Type Description errorMongoError An error instance representing the error during the execution.
clientMongoClient The connected client.