Releases: mongodb/node-mongodb-native
v4.6.0
The MongoDB Node.js team is pleased to announce version 4.6.0 of the mongodb package!
Release Highlights
TypeScript: ChangeStreamDocument
Our change stream document type and watch API have undergone some improvements! You can now define your own custom type for the top level document returned in a 'change'
event. This is very useful when using a pipeline that significantly changes the shape of the change document (ex. $replaceRoot
, $project
operators). Additionally, we've improved the type information of the default change stream document to default to union of the possible events from MongoDB. This works well with typescript's ability to narrow a Discriminated Union based on the operationType
key in the default change stream document.
Prior to this change the ChangeStreamDocument
inaccurately reflected the runtime shape of the change document. Now, using the union, we correctly indicate that some properties do not exist at all on certain events (as opposed to being optional). With this typescript fix we have added the properties to
for rename events, as well as lsid
, txnNumber
, and clusterTime
if the change is from within a transaction.
NOTE: Updating to this version may require fixing typescript issues. Those looking to adopt this version but defer any type corrections can use the watch API like so: .watch<any, X>()
. Where X
controls the type of the change document for your use case.
Check out the examples and documentation here.
Performance: Consider Server Load During Server Selection
Operations will now be directed towards servers that have fewer in progress operations. This distributes load across servers and prevents overwhelming servers that are already under load with additional requests.
Note
This release includes some experimental features that are not yet ready for use. As a reminder, anything marked experimental is not a part of the official driver API and is subject to change without notice.
Features
- NODE-2992: consider server load during server selection (#3219) (35eeba3)
- NODE-4059: ChangeStreamDocument not fully typed to specification (#3191) (8b24212)
Bug Fixes
- NODE-3565: Improve error message for insertMany with partially empty array (#3221) (0ef2516)
- NODE-4232: stream() also returns generic AsyncIterable (ed4ba58)
- NODE-3688: make handshake errors retryable (#3165) (3f8765a)
- NODE-3833: return early on end if gridfs upload stream is already ended (#3223) (c27e844)
- NODE-3928: don't throw error in Response constructor (#3199) (441fc63)
- NODE-4031: options parsing for array options (#3193) (4b2e3d1)
- NODE-4133: array field NestedPaths return type (#3184) (c46c984)
- NODE-4156: remove comment from commands pre-4.4 (#3195) (4e6dccd)
- NODE-4188: default localThresholdMS to 15ms (#3207) (5e730ff)
- NODE-4208: add aws http request timeout handler (#3225) (829d7be)
Documentation
- Reference: https://docs.mongodb.com/drivers/node/current/
- API: https://mongodb.github.io/node-mongodb-native/4.6
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.6.0-alpha.0
The MongoDB Node.js team is pleased to announce version v4.6.0-alpha.0 of the mongodb package!
Release Highlights
This release is for internal testing - NOT intended for use production.
Features
Bug Fixes
Documentation
v4.5.0
The MongoDB Node.js team is pleased to announce version 4.5.0 of the mongodb package!
Release Highlights
This release includes a number of enhancements noted below.
comment
option support
The comment
option is now widely available: by setting a comment
on an operation you can trace its value in database logs for more insights.
collection.insertOne(
{ name: 'spot' },
{ comment: { started: new Date() } }
)
An example of a log line, trimmed for brevity. We can see the timestamp of the log and the time created on our client application differ.
{
"t": { "$date": "2022-04-04T16:08:56.079-04:00" },
"attr": {
"commandArgs": {
"documents": [ { "_id": "...", "name": "spot" } ],
"comment": { "started": { "$date": "2022-04-04T20:08:56.072Z" } } }
}
}
Socket timeout fixes for FaaS environments
This release includes a fix for serverless environments where transient serverHeartBeatFailure
events that could be corrected to serverHeartBeatSucceeded
events in the next tick of the event loop were nonetheless handled as an actual issue with the client's connection and caused unnecessary resource clean up routines.
It turns out that since Node.js handles timeout events first in the event loop, socket timeouts expire while the FaaS environment is dormant and the timeout handler code is the first thing that runs upon function wake prior to checking for any data from the server. Delaying the timeout handling until after the data reading phase avoids the sleep-induced timeout error in the cases where the connection is still healthy.
TS fixes for 4.7
Typescript 4.7 may not be out yet but in preparation for its release we've fixed issues compiling against that version. The main new obstacle was defaulting generic arguments that require that the constraining condition enforce similarity with the defaulted type. You may notice that our change stream watch<T extends Document = Document>()
methods now requires that T
extends Document, a requirement that already had to be met by the underlying ChangeStreamDocument
type.
Features
- NODE-3697: reduce serverSession allocation (#3171) (5132bc9)
- NODE-3699: add support for
comment
field (#3167) (4e2f9bf) - NODE-4014: Add let option to bulk write operations (#3160) (6f633d1)
Bug Fixes
- NODE-3769: retryable writes are not compliant with specification (#3144) (ff26b12)
- NODE-3810: delay timeout errors by one event loop tick (#3180) (0ed7cbf)
- NODE-4069: remove 'default' from options for fullDocument field in change stream options (#3169) (799689e)
- NODE-4074: ensure getTopology doesn't throw synchronously (#3172) (329f081)
- NODE-4129: constrain
watch
type parameter to extendChangeStream
type parameter (#3183) (43ba9fc)
Documentation
- Reference: https://docs.mongodb.com/drivers/node/current/
- API: https://mongodb.github.io/node-mongodb-native/4.5
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.4.1
The MongoDB Node.js team is pleased to announce version 4.4.1 of the mongodb package!
Bug Fixes
- NODE-3521: update session support checks (#3151) (aaa453d)
- NODE-3948: Add error code to MongoSystemError (#3149) (446da95)
Documentation
- Reference: https://docs.mongodb.com/drivers/node
- API: https://mongodb.github.io/node-mongodb-native/4.4
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.4.0
The MongoDB Node.js team is pleased to announce version 4.4.0 of the mongodb package!
Release Highlights
This release includes a few new features described below.
KMIP
KMIP can now be configured as a KMS provider for CSFLE by providing the KMIP endpoint in the kmsProviders option.
Example:
new MongoClient(uri, { autoEncryption: { kmsProviders: { kmip: { endpoint: 'host:port' }}}})
CSFLE TLS
Custom TLS options can now be provided for connection to the KMS servers on a per KMS provider basis.
Example:
new MongoClient(uri, { autoEncryption: { tlsOptions: { aws: { tlsCAFile: 'path/to/file' }}}})
Valid options are tlsCAFile
, tlsCertificateKeyFile
, tlsCertificateKeyFilePassword
and all accept strings as values: a string path to a certificate location on the file system or a string password.
Kerberos
Hostname canonicalization when using GSSAPI authentication now accepts 'none'
, 'forward'
, and 'forwardAndReverse'
as auth mechanism properties. 'none'
will perform no canonicalization (default), 'forward'
will perform a forward cname lookup, and 'forwardAndReverse'
will perform a forward lookup followed by a reverse PTR lookup on the IP address. Previous boolean values are still accepted and map to false -> 'none'
and true -> 'forwardAndReverse'
.
Example:
new MongoClient('mongodb://user:pass@host:port/db?authMechanism=GSSAPI&authMechanismProperties=CANONICALIZE_HOST_NAME=forward');
For cases when the service host name differs from the connection’s host name (most likely when creating new users on localhost), a SERVICE_HOST
auth mechanism property may now be provided.
Example:
new MongoClient('mongodb://user:pass@host:port/db?authMechanism=GSSAPI&authMechanismProperties=SERVICE_HOST:example.com')
⚠️ collection.count() and cursor.count()
In the 4.0.0 release of the driver, the deprecated collection.count()
method was inadvertently changed to behave like collection.countDocuments()
. In this release, we have updated the collection.count()
behavior to match the legacy behavior:
- If a query is passed in,
collection.count
will behave the same ascollection.countDocuments
and perform a collection scan. - If no query is passed in,
collection.count
will behave the same ascollection.estimatedDocumentCount
and rely on collection metadata.
We also deprecated the cursor.count()
method and will remove it in the next major version along with collection.count()
; please use collection.estimatedDocumentCount()
or collection.countDocuments()
instead.
Features
- NODE-2938: add service host mechanism property (#3130) (46d5821)
- NODE-2939: add new hostname canonicalization opts (#3131) (d0390d0)
- NODE-3351: use hostname canonicalization (#3122) (f5c76f3)
- NODE-3777: add csfle kmip support (#3070) (44bbd6e)
- NODE-3867: deprecate cursor count and update v4 docs (#3127) (a48d7e2)
Bug Fixes
- NODE-3621: fixed type of documentKey property on ChangeStreamDocument (#3118) (c63a21b)
- NODE-3795: unexpected No auth provider for DEFAULT defined error (#3092) (fb38a56)
- NODE-3813: unexpected type conversion of read preference tags (#3138) (3e7b894)
- NODE-3878: use legacy count operation on collection.count (#3126) (12c6835)
- NODE-3917: Throw an error when directConnection is set with multiple hosts (#3143) (b192493)
Documentation
- Reference: https://docs.mongodb.com/drivers/node
- API: https://mongodb.github.io/node-mongodb-native/4.4
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.3.1
The MongoDB Node.js team is pleased to announce version 4.3.1 of the mongodb package!
Release Highlights
In this patch release, we address the limitation introduced in 4.3.0 with the dot notation Typescript improvements and recursive types.
Namely, this fix removes compilation errors for self-referential types.
Note that this fix still has the following limitations:
- type checking defaults to
any
after the first level of recursion for self-referential types
interface Node {
next: Node | null;
}
declare const collection: Collection<Node>;
// no error here even though `next` is of type `Node | null`
collection.find({
next: {
next: 'asdf'
}
});
- indirectly self-referential types are still not supported
interface A {
b: B;
}
interface B {
a: A;
}
declare const mutuallyRecursive: Collection<A>;
// this will throw an error because there is indirect recursion
// between types (A depends on B which depends on A and so on)
mutuallyRecursive.find({});
Bug Fixes
- NODE-3792: remove offensive language throughout the codebase (#3091) (8e2b0cc)
- NODE-3852,NODE-3854,NODE-3856: Misc typescript fixes for 4.3.1 (#3102) (dd5195a)
Documentation
- Reference: https://docs.mongodb.com/drivers/node
- API: https://mongodb.github.io/node-mongodb-native/4.3
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.3.0
The MongoDB Node.js team is pleased to announce version 4.3.0 of the mongodb package!
Release Highlights
This release includes SOCKS5 support and a couple of other important features and bug fixes that we hope will improve your experience with the node driver.
The SOCKS5 options can be configured via the proxyHost
, proxyPort
, proxyPassword
and proxyUsername
options in the connection string passed to the MongoClient
instance. Big thanks to @addaleax for helping with this feature!
The other notable features address performance and TypeScript as detailed below.
Performance
The original release of the 4.x driver relied on a new version of the BSON library that enables UTF-8 validation by default, resulting in noticeable performance degradation over the 3.x driver when processing over string data. This release introduces an option to opt out of this validation by specifying enableUtf8Validation: false
at the client, database, collection, or individual operation level.
For example:
// disable UTF-8 validation globally on the MongoDB client
const client = new MongoClient('mongodb://localhost:27017', { enableUtf8Validation: false });
// disable UTF-8 validation for a particular operation
const client = new MongoClient('mongodb://localhost:27017');
const db = client.db('database name');
const collection = db.collection('collection name');
await collection.find({ name: 'John Doe'}, { enableUtf8Validation: false });
TypeScript
Type inference for nested documents
Thanks to an amazing contribution from @avaly we now have support for key auto-completion and type hinting on nested documents! MongoDB permits using dotted keys to reference nested keys or specific array indexes within your documents as a shorthand for getting at keys beneath the top layer. Typescript's Template Literal types allow us to take the interface defined on a collection and calculate at compile time the nested keys and indexes available.
For example:
interface Human {
name: string;
age: number;
}
interface Pet {
name: string
bestFriend: Human
}
const pets = client.db().collection<Pet>('pets');
await pets.findOne({ 'bestFriend.age': 'young!' }) // typescript error!
Here's what autocomplete suggests in VSCode:
WARNING: There is a known shortcoming to this feature: recursive types can no longer be used in your schema. For example, an interface that references itself or references another schema that references back to the root schema cannot be used on our Collection
generic argument. Unlike at runtime where a "recursive" shaped document has an eventual stopping point we don't have the tools within the language to declare a base case enumerating nested keys. We hope this does not cause friction when upgrading driver versions: please do not hesitate to reach out with any feedback you have about this feature.
Consistent type inference for the _id type
We have also enhanced the type inference for the _id
type. Now, when performing operations on a collection, the following holds true based on the type of the schema:
- If no
_id
is specified on the schema, it is inferred to be of typeObjectId
and is optional on inserts. - If an
_id
is specified on the schema as required, then the_id
type is inferred to be of the specified type and is required on inserts. - If an
_id
is specified on the schema as optional, it is inferred to be of the specified type and is optional on inserts: this format is intended to be used with thepkFactory
option in order to ensure a consistent_id
is assigned to every new document.
Features
- NODE-3589: support dot-notation attributes in Filter (#2972) (76fff97)
- NODE-3633: add Socks5 support (#3041) (451627a)
- NODE-3784: Add
enableUtf8Validation
option (#3074) (4f56409)
Bug Fixes
- gridfs: make
GridFSBucketWriteStream.prototype.end()
returnthis
for compat with @types/node@17.0.6 (#3088) (7bb9e37) - NODE-2899: sort and correct circular imports (#3072) (48cc729)
- NODE-3675: SRV option bug correctly defaults authSource to $external (#3079) (30f2a2d)
- NODE-3803: Fix _id typing on collection create operations (#3077) (f1979db)
Documentation
- Reference: https://docs.mongodb.com/drivers/node
- API: https://mongodb.github.io/node-mongodb-native/4.3
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.2.2
The MongoDB Node.js team is pleased to announce version 4.2.2 of the mongodb package!
Bug Fixes
- NODE-3705: ReadPreference.fromOptions omitting hedge and maxStalenessSeconds when readPreference is a string (#3060) (b9fbac5)
- NODE-3711: retry txn end on retryable write (#3045) (7b00d0f)
- NODE-3765: make replacement for replaceOne operations without _id (#3040) (e07e564)
- stricter protocol check in connection string (#3078) (bc05671)
Documentation
- Reference: https://docs.mongodb.com/drivers/node
- API: https://mongodb.github.io/node-mongodb-native/4.2
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.2.1
The MongoDB Node.js team is pleased to announce version 4.2.1 of the mongodb package!
Release Highlights
This release fixes an issue with the dbName being overridden by the authSource option. Additionally, we have ensured that cursors re-run server selection when fetching additional batches, which should reduce issues encountered in long running function as a service environments.
Bug Fixes
- NODE-2370: correct a return type of hasNext() (#3058) (b6a63df)
- NODE-3627: Enable flexible BSON validation for server error key containing invalid utf-8 (#3054) (7a507f0)
- NODE-3648: run get more ops through server selection (#3030) (268e211)
- NODE-3767: don't delete dbName if authSource is provided (#3055) (0a830e2)
- NODE-3770: Filter type uses WithId on the schema (#3053) (307d623)
Documentation
- Reference: https://docs.mongodb.com/drivers/node
- API: https://mongodb.github.io/node-mongodb-native/4.2
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.
v4.2.0
Release Highlights
This release includes a number of features we’re happy to announce. You can now run aggregation pipelines that write write to a MongoDB collection using $out and $merge stages on secondaries! We’ve added an option to limit the number of hosts the driver will connect to when using SRV DNS lookups to manage your host addresses. And lastly, the authorizedCollection
option is now usable on the db.listCollections()
function.
Additionally, in this release, we’ve marked collection.mapReduce()
as deprecated. The same functionality can be replicated in the much more flexible aggregation pipeline. Visit Map-Reduce to Aggregation Pipeline to learn more.
The minimum supported MongoDB version is 3.6. Attempts to connect to a MongoDB server older than 3.6 will result in an error.
Please take note of the MongoDB Software Lifecycle Schedules for timeframes of supported server versions.
Features
- NODE-3083: support aggregate writes on secondaries (#3022) (f696909)
- NODE-3446: deprecate mapReduce command (#3036) (b6c73bf)
- NODE-3467: implement srvMaxHosts, srvServiceName options (#3031) (1f8b539)
- NODE-3469,NODE-3615,NODE-3507: update min and max wire versions (#3014) (2a78d5a)
- NODE-3691: make time series options granularity type strict (#3005) (98017f9)
- NODE-3692: make change stream events typing more generic (#3034) (d5ae78e)
- NODE-3728: Allow to pass
authorizedCollections
option to thedb.listCollections
method (#3021) (e1234a7) - NODE-3729: add withId to default return type for collection.find and collection.findOne (#3039) (52520aa)
Bug Fixes
- NODE-3116: reschedule unreliable async interval first (#3006) (33886a7)
- NODE-3344: allow setting
defaultTransactionOptions
with POJO rather than ReadConcern instance (#3032) (53b3164) - NODE-3515: do proper opTime merging in bulk results (#3012) (43300c3)
- NODE-3668: compile error with OptionalId on TS 4.5 beta (#3004) (ee7f095)
- NODE-3726: add optional option overloads of Db's createCollection function (#3019) (c3149e1)
- NODE-3727: add overloads for BulkOperationBase's execute function (#3018) (216d194)
Documentation
- Reference: https://docs.mongodb.com/drivers/node
- API: https://mongodb.github.io/node-mongodb-native/4.2
- Changelog: https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md
We invite you to try the mongodb library immediately, and report any issues to the NODE project.