Creates a new Db instance.
Db name cannot contain a dot, the server may apply more restrictions when an operation is run.
The MongoClient for the database.
The name of the database this instance represents.
Optionaloptions: DbOptionsOptional settings for Db construction.
StaticSYSTEM_StaticSYSTEM_StaticSYSTEM_StaticSYSTEM_StaticSYSTEM_StaticSYSTEM_The current readPreference of the Db. If not explicitly defined for this Db, will be inherited from the parent MongoClient
Check if a secondary can be used (because the read preference is not set to primary)
Execute an aggregation framework pipeline against the database.
An array of aggregation stages to be executed
Optionaloptions: AggregateOptionsOptional settings for the command
Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.
Collection namespace validation is performed server-side.
the collection name we wish to access.
return the new Collection instance
Fetch all collections for the current db.
Optionaloptions: ListCollectionsOptionsOptional settings for the command
Execute a command
The command to run
Optionaloptions: { Optional settings for the command
This command does not inherit options from the MongoClient.
The driver will ensure the following fields are attached to the command sent to the server:
lsid - sourced from an implicit session or options.session$readPreference - defaults to primary or can be configured by options.readPreference$db - sourced from the name of this databaseIf the client has a serverApi setting:
apiVersionapiStrictapiDeprecationErrorsWhen in a transaction:
readConcern - sourced from readConcern set on the TransactionOptionswriteConcern - sourced from writeConcern set on the TransactionOptionsAttaching any of the above fields to the command will have no effect as the driver will overwrite the value.
Create a new collection on a server with the specified options. Use this to create capped collections. More information about command options available at https://www.mongodb.com/docs/manual/reference/command/create/
Collection namespace validation is performed server-side.
The name of the collection to create
Optionaloptions: CreateCollectionOptionsOptional settings for the command
Creates an index on the db and collection.
Name of the collection to create the index on.
Specify the field to index, or an index specification
Optionaloptions: CreateIndexesOptionsOptional settings for the command
Drop a collection from the database, removing it permanently. New accesses will create a new collection.
Name of collection to drop
Optionaloptions: DropCollectionOptionsOptional settings for the command
Drop a database, removing it permanently from the server.
Optionaloptions: CommandOperationOptionsOptional settings for the command
Retrieves this collections index info.
The name of the collection.
Optional settings for the command
List all collections of this database with optional filter
Query to filter collections by
Optional settings for the command
Optionalfilter: DocumentOptionaloptions: ListCollectionsOptions & AbortableRetrieve the current profiling Level for MongoDB
Optionaloptions: CommandOperationOptionsOptional settings for the command
Remove a user from a database
The username to remove
Optionaloptions: CommandOperationOptionsOptional settings for the command
Rename a collection.
Name of current collection to rename
New name of of the collection
Optionaloptions: RenameOptionsOptional settings for the command
A low level cursor API providing basic driver functionality:
The command that will start a cursor on the server.
Optionaloptions: RunCursorCommandOptionsConfigurations for running the command, bson options will apply to getMores
Set the current profiling level of MongoDB
The new profiling level (off, slow_only, all).
Optionaloptions: CommandOperationOptionsOptional settings for the command
Get all the db statistics.
Optionaloptions: DbStatsOptionsOptional settings for the command
Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this database. Will ignore all changes to system collections.
Type of the data being detected by the change stream
Type of the whole change stream document emitted
An array of pipeline stages through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
Optional settings for the command
watch() accepts two generic arguments for distinct use cases:
In iterator mode, if a next() call throws a timeout error, it will attempt to resume the change stream. The next call can just be retried after this succeeds.
const changeStream = collection.watch([], { timeoutMS: 100 });
try {
await changeStream.next();
} catch (e) {
if (e instanceof MongoOperationTimeoutError && !changeStream.closed) {
await changeStream.next();
}
throw e;
}
In emitter mode, if the change stream goes timeoutMS without emitting a change event, it will
emit an error event that returns a MongoOperationTimeoutError, but will not close the change
stream unless the resume attempt fails. There is no need to re-establish change listeners as
this will automatically continue emitting change events once the resume attempt completes.
const changeStream = collection.watch([], { timeoutMS: 100 });
changeStream.on('change', console.log);
changeStream.on('error', e => {
if (e instanceof MongoOperationTimeoutError && !changeStream.closed) {
// do nothing
} else {
changeStream.close();
}
});
The Db class is a class that represents a MongoDB Database.
Example