Creates a new Db instance
| Name | Type | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
databaseName |
string |
The name of the database this instance represents. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
topology |
Server | ReplSet | Mongos |
The server topology for the database. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
object | null |
optional
Optional settings.
|
Properties:
| Name | Type | Description |
|---|---|---|
serverConfig |
Server | ReplSet | Mongos | Get the current db topology. |
bufferMaxEntries |
number | Current bufferMaxEntries value for the database |
databaseName |
string | The name of the database this instance represents. |
options |
object | The options associated with the db instance. |
native_parser |
boolean | The current value of the parameter native_parser. |
slaveOk |
boolean | The current slaveOk value for the db instance. |
writeConcern |
object | The current write concern values. |
topology |
object | Access the topology object (single server, replicaset or mongos). |
Fires:
- Db#event:close
- Db#event:reconnect
- Db#event:error
- Db#event:timeout
- Db#event:parseError
- Db#event:fullsetup
Returns:
Db instance.Methods
-
Add a user to the database.
NOTE: if you are connecting to MongoDB >= 4.0, the password will not be digested.
We STRONGLY recommend that adding users be done exclusively over a TLS connection.Name Type Default Description usernamestring The username.
passwordstring The password.
optionsobject null optional Optional settings.
Name Type Default Description wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
customDataobject null optional Custom data associated with the user (only Mongodb 2.6 or higher)
rolesArray.<object> null optional Roles associated with the created user (only Mongodb 2.6 or higher)
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
Return the Admin db instance
Returns:
the new Admin db instance
-
collection(name, options, callback){Collection}
-
Fetch a specific collection (containing the actual collection information). If the application does not use strict mode you
can use it without a callback in the following way:var collection = db.collection('mycollection');Name Type Default Description namestring the collection name we wish to access.
optionsobject null optional Optional settings.
Name Type Default Description wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
rawboolean false optional Return document results as raw BSON buffers.
pkFactoryobject null optional A primary key factory object for generation of custom _id keys.
readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
serializeFunctionsboolean false optional Serialize functions on any object.
strictboolean false optional Returns an error if the collection does not exist
readConcernobject null optional Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
Name Type Default Description levelobject 'local' optional Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported)
callbackDb~collectionResultCallback optional The collection result callback
Returns:
the new Collection instance if not in strict mode
-
Fetch all collections for the current db.
Name Type Description optionsObject optional Optional settings
Name Type Description sessionClientSession optional optional session to use for this operation
callbackDb~collectionsResultCallback optional The results callback
Returns:
Promise if no callback passed
-
Execute a command
Name Type Default Description commandobject The command hash
optionsobject null optional Optional settings.
Name Type Default Description readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
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/Name Type Default Description namestring the collection name we wish to access.
optionsobject null optional Optional settings.
Name Type Default Description wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
rawboolean false optional Return document results as raw BSON buffers.
pkFactoryobject null optional A primary key factory object for generation of custom _id keys.
readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
serializeFunctionsboolean false optional Serialize functions on any object.
strictboolean false optional Returns an error if the collection does not exist
cappedboolean false optional Create a capped collection.
autoIndexIdboolean true optional DEPRECATED: Create an index on the _id field of the document, True by default on MongoDB 2.6 - 3.0
sizenumber null optional The size of the capped collection in bytes.
maxnumber null optional The maximum number of documents in the capped collection.
flagsnumber null optional Optional. Available for the MMAPv1 storage engine only to set the usePowerOf2Sizes and the noPadding flag.
storageEngineobject null optional Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection on MongoDB 3.0 or higher.
validatorobject null optional Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation on MongoDB 3.2 or higher.
validationLevelstring null optional Determines how strictly MongoDB applies the validation rules to existing documents during an update on MongoDB 3.2 or higher.
validationActionstring null optional Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted on MongoDB 3.2 or higher.
indexOptionDefaultsobject null optional Allows users to specify a default configuration for indexes when creating a collection on MongoDB 3.2 or higher.
viewOnstring null optional The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create on MongoDB 3.4 or higher.
pipelinearray null optional An array that consists of the aggregation pipeline stage. create creates the view by applying the specified pipeline to the viewOn collection or view on MongoDB 3.4 or higher.
collationobject null optional Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
sessionClientSession optional optional session to use for this operation
callbackDb~collectionResultCallback optional The results callback
Returns:
Promise if no callback passed
-
Creates an index on the db and collection collection.
Name Type Default Description namestring Name of the collection to create the index on.
fieldOrSpecstring | object Defines the index.
optionsobject null optional Optional settings.
Name Type Default Description wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
uniqueboolean false optional Creates an unique index.
sparseboolean false optional Creates a sparse index.
backgroundboolean false optional Creates the index in the background, yielding whenever possible.
dropDupsboolean false optional A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
minnumber null optional For geospatial indexes set the lower bound for the co-ordinates.
maxnumber null optional For geospatial indexes set the high bound for the co-ordinates.
vnumber null optional Specify the format version of the indexes.
expireAfterSecondsnumber null optional Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
namenumber null optional Override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
partialFilterExpressionobject null optional Creates a partial index based on the given filter object (MongoDB 3.2 or higher)
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
Drop a collection from the database, removing it permanently. New accesses will create a new collection.
Name Type Description namestring Name of collection to drop
optionsObject optional Optional settings
Name Type Description sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The results callback
Returns:
Promise if no callback passed
-
Drop a database, removing it permanently from the server.
Name Type Description optionsObject optional Optional settings
Name Type Description sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The results callback
Returns:
Promise if no callback passed
-
Ensures that an index exists, if it does not it creates it
Name Type Default Description namestring The index name
fieldOrSpecstring | object Defines the index.
optionsobject null optional Optional settings.
Name Type Default Description wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
uniqueboolean false optional Creates an unique index.
sparseboolean false optional Creates a sparse index.
backgroundboolean false optional Creates the index in the background, yielding whenever possible.
dropDupsboolean false optional A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
minnumber null optional For geospatial indexes set the lower bound for the co-ordinates.
maxnumber null optional For geospatial indexes set the high bound for the co-ordinates.
vnumber null optional Specify the format version of the indexes.
expireAfterSecondsnumber null optional Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
namenumber null optional Override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
- Deprecated
- since version 2.0
Returns:
Promise if no callback passed
-
Evaluate JavaScript on the server
Name Type Default Description codeCode JavaScript to execute on server.
parametersobject | array The parameters for the call.
optionsobject null optional Optional settings.
Name Type Default Description nolockboolean false optional Tell MongoDB not to block on the evaulation of the javascript.
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The results callback
- Deprecated
- Eval is deprecated on MongoDB 3.2 and forward
Returns:
Promise if no callback passed
-
Runs a command on the database as admin.
Name Type Default Description commandobject The command hash
optionsobject null optional Optional settings.
Name Type Default Description readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
Retrieves this collections index info.
Name Type Default Description namestring The name of the collection.
optionsobject null optional Optional settings.
Name Type Default Description fullboolean false optional Returns the full raw index information.
readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
listCollections(filter, options){CommandCursor}
-
Get the list of all collection information for the specified db.
Name Type Default Description filterobject {} optional Query to filter collections by
optionsobject null optional Optional settings.
Name Type Default Description batchSizenumber null optional The batchSize for the returned command cursor or if pre 2.8 the systems batch collection
readPreferenceReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
sessionClientSession optional optional session to use for this operation
-
Retrive the current profiling information for MongoDB
Name Type Description optionsObject optional Optional settings
Name Type Description sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback.
- Deprecated
- Query the system.profile collection directly.
Returns:
Promise if no callback passed
-
Retrieve the current profiling Level for MongoDB
Name Type Description optionsObject optional Optional settings
Name Type Description sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
Remove a user from a database
Name Type Default Description usernamestring The username.
optionsobject null optional Optional settings.
Name Type Default Description wnumber | string null optional The write concern.
wtimeoutnumber null optional The write concern timeout.
jboolean false optional Specify a journal write concern.
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
Rename a collection.
Name Type Default Description fromCollectionstring Name of current collection to rename.
toCollectionstring New name of of the collection.
optionsobject null optional Optional settings.
Name Type Default Description dropTargetboolean false optional Drop the target name collection if it previously exists.
sessionClientSession optional optional session to use for this operation
callbackDb~collectionResultCallback optional The results callback
Returns:
Promise if no callback passed
-
Set the current profiling level of MongoDB
Name Type Description levelstring The new profiling level (off, slow_only, all).
optionsObject optional Optional settings
Name Type Description sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The command result callback.
Returns:
Promise if no callback passed
-
Get all the db statistics.
Name Type Default Description optionsobject null optional Optional settings.
Name Type Default Description scalenumber null optional Divide the returned sizes by scale value.
sessionClientSession optional optional session to use for this operation
callbackDb~resultCallback optional The collection result callback
Returns:
Promise if no callback passed
-
Unref all sockets
Type Definitions
-
The callback format for the collection method, must be used if strict is specified
Name Type Description errorMongoError An error instance representing the error during the execution.
collectionCollection The collection instance.
-
The callback format for results
Name Type Description errorMongoError An error instance representing the error during the execution.
resultobject The result object if the command was executed successfully.
Events
-
Db close event
Emitted after a socket closed against a single server or mongos proxy.
Type:
-
Db error event
Emitted after an error occurred against a single server or mongos proxy.
Type:
-
Db fullsetup event, emitted when all servers in the topology have been connected to at start up time.
- Server: Emitted when the driver has connected to the single server and has authenticated.
- ReplSet: Emitted after the driver has attempted to connect to all replicaset members.
- Mongos: Emitted after the driver has attempted to connect to all mongos proxies.
Type:
-
Db parseError event
The parseError event is emitted if the driver detects illegal or corrupt BSON being received from the server.
Type:
-
Db reconnect event
- Server: Emitted when the driver has reconnected and re-authenticated.
- ReplicaSet: N/A
- Mongos: Emitted when the driver reconnects and re-authenticates successfully against a Mongos.
Type:
- object
-
Db timeout event
Emitted after a socket timeout occurred against a single server or mongos proxy.
Type: