Releases: mongodb/node-mongodb-native
4.11.0
The MongoDB Node.js team is pleased to announce version 4.11.0 of the mongodb package!
Release Highlights
Recursive Schema Support
Version 4.3.0 of the Node driver added Typescript support for dot notation into our Filter
type but
in the process it broke support for recursive schemas. In 4.11.0, we now support mutually recursive schemas and
provide type safety on dot notation queries up to a depth of 8. Beyond a depth of 8, code still compiles
but is no longer type checked (it falls back to a type of any
).
interface Author {
name: string;
bestBook: Book;
}
interface Book {
title: string;
author: Author;
}
let authors: Collection<Author>
// below a depth of 8, type checking is enforced
authors.findOne({ 'bestBook.author.bestBook.title': 25 }})
// ✅ expected compilation error is thrown: "title must be a string"
// at a depth greater than 8 code compiles but is not type checked (9 deep in this example)
authors.findOne({ 'bestBook.author.bestBook.author.bestBook.author.bestBook.author.name': 25 })
// ⛔️ perhaps unexpected, no compilation error is thrown because the key is too deeply nested
Note that our depth limit is a product of Typescript's recursive type limitations.
AWS Authentication
If the optional aws-sdk dependency is installed, the driver will now use the SDK to get credentials
from the environment. Because of this, if you have a shared AWS credentials or config file, then
those credentials will be used by default if AWS auth environment variables are not set. To override this
behavior, set AWS_SHARED_CREDENTIALS_FILE=""
in your shell or set the
equivalent environment variable value in your script or application. Alternatively, you can create
an AWS profile specifically for your MongoDB credentials and set the AWS_PROFILE
environment
variable to that profile name.
External Contributions
Many thanks to those who contributed to this release!
- @ermik provided an extremely large schema to test compilation with, which made testing our new recursive schema support possible with large schemas straightforward.
- @noahsilas for documentation improvements in change streams and fixing our Typescript types for read preferences.
- @zendagin for adding Typescript support for hashed indexes.
- @biniona-mongodb for fixing our parsing of TLS options.
- @LinusU for removing support for server versions lower than our minimum supported server version and improving error messages for unacknowledged writes with hints.
Features
- NODE-3651: add hashed index type (#3432) (f6b56a1)
- NODE-3875: support recursive schema types (#3433) (26bce4a)
- NODE-4503: throw original error when server attaches NoWritesPerformed label (#3441) (a7dab96)
- NODE-4650: handle handshake errors with SDAM (#3426) (cbe7533)
- NODE-4721: add aws-sdk as optional dependency (#3446) (b879cb5)
Bug Fixes
- NODE-3712,NODE-4546: electionId should be ordered before setVersion (#3174) (ca51fec)
- NODE-3921: error on invalid TLS option combinations (#3405) (1a550df)
- NODE-4186: accept ReadPreferenceLike in TransactionOptions type (#3425) (dc62bcb)
- NODE-4475: make interrupted message more specific (#3437) (5f37cb6)
- NODE-4608: prevent parallel monitor checks (#3404) (78bcfe4)
- NODE-4647: improve error message (#3409) (0d3c02e)
- NODE-4649: use SDAM handling for errors from min pool size population (#3424) (ef3b55d)
Documentation
- Reference: https://docs.mongodb.com/drivers/node/current/
- API: https://mongodb.github.io/node-mongodb-native/4.11
- 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.10.0
The MongoDB Node.js team is pleased to announce version 4.10.0 of the mongodb package!
Release Highlights
Callback Deprecation
Looking to improve our API's consistency and handling of errors we are planning to remove callback support in the next major release of the driver. Today marks the notice of their removal. Migrating to a promise only API allows us to offer uniform error handling and better native support for automatic promise construction. In this release you will notice deprecation warnings in doc comments for all our callback overloads and if you are working in VSCode you should notice strikethroughs on these APIs. We encourage you to migrate to promises where possible:
- Using
async
/await
syntax can yield the best experience with promise usage. - Using Node.js' callbackify utility is one approach:
require('util').callbackify(() => collection.findOne())(callback)
- Using .then syntax is another:
collection.findOne().then(res => callback(null, res), err => callback(err))
- And lastly, for large codebases still intertwined with callbacks we have an alternative package prepared.
MongoDB-Legacy Callback Support
While the 4.10.0 version only deprecates our support of callbacks, there will be a major version that removes the support altogether. In order to keep using callbacks after v5 is released, we recommend migrating your driver version to mongodb-legacy (github link). This package wraps every single async API our driver offers and is designed to provide the exact behavior of the MongoDB 4.10.0 release (both callbacks and promises are supported). Any new features added to MongoDB will be automatically inherited but will only support promises. This package is fully tested against our current suite and adoption should be confined to changing an import require('mongodb')
-> require('mongodb-legacy')
. If this package is useful to you and your use case we encourage you to adopt it before v5 to ensure it continues to work as expected.
Read more about it on the package's readme here:
Features
- NODE-4385: add cmap pool pausing functionality (#3321) (335ee55)
- NODE-4484: add experimental support for disambiguatedPaths in change stream documents (#3365) (846365a)
- NODE-4519: deprecate promiseLibrary and PromiseProvider (#3403) (5c322b6)
- NODE-4547: mark all callback APIs as deprecated (#3388) (a983f14)
- NODE-4634: add support for bulk FindOperators.hint() (#3408) (8758890)
Bug Fixes
- NODE-3144: pool clear event ordering and retryability tests (#3407) (bdc0d67)
- NODE-4557: randomize servers when there are only 2 eligible servers (#3390) (ddcfa49)
- NODE-4583: revert nested union type support (#3383) (7f94f0a)
- NODE-4591: only set loadBalanced on handshake when explicitly set (#3386) (57e9f2d)
- NODE-4621: ipv6 address handling in HostAddress (#3410) (5eb3978)
- NODE-4639: allow PromiseProvider to be null (#3412) (d29b3d9)
Documentation
- Reference: https://docs.mongodb.com/drivers/node/current/
- API: https://mongodb.github.io/node-mongodb-native/4.10
- 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.9.1
The MongoDB Node.js team is pleased to announce version 4.9.1 of the mongodb package!
Release Highlights
This is a bug fix release as noted below.
Bug Fixes
v4.9.0
The MongoDB Node.js team is pleased to announce version 4.9.0 of the mongodb package!
Release Highlights
We have corrected an inconsistency with our writeConcern options in the type definitions where the MongoClient alleged to not support "writeConcern" as an option. In fact, it did support it at run time and now the types correctly reflect that, along with the corresponding deprecations we made to the nested writeConcern config settings.
Our index specification handling had a few peculiar edge cases that we have detailed below, we believe these are unlikely to affect a vast majority of users as the type definitions would have likely reported an error with the impacted usage. As a feature, the typescript definitions now support a javascript Map as a valid input for an index specification.
Index Specification Detailed Fixes
- Map as a valid input type in TS definition
- Uses Map under the hood to ensure key order is preserved, fixed numeric index key order issue in combination with FLE usage
- Tuples passed at the top level to
createIndex
were incorrectly parsed as string inputcreateIndex(['myKey', 1])
would create{ 'myKey': 1, '1': 1 }
.- Now it's correctly detected if the second arg is one of the known index directions.
- For complex programmatic generation of indexes we recommend using a Map to avoid all the edge cases here.
- Type strictness on this nesting of array (one or more)
- Type strictness for createIndexes aligned with createIndex
- No longer accepts just Document, checks that the values are a known IndexDirection
As per usual this release brings in the latest BSON release (v4.7.0) which added automatic UUID support. You can read more about that in the BSON release notes here!
Special thanks to the folks who contributed to this release!
- @sampaiodiego for the
oplogReplay
flag support fix - @jer-sen for typescript Filter definition improvements
- @aditi-khare-mongoDB for index specification fixes / type improvements
Features
- NODE-3517: improve index spec handling and type definitions (#3315) (0754bf9)
- NODE-4336: deprecate old write concern options and add missing writeConcern to MongoClientOptions (#3340) (d2b6ad8)
Bug Fixes
- NODE-4159,NODE-4512: remove servers with incorrect setName from topology and fix unix socket parsing (#3348) (00dcf2d)
- NODE-4273: pass 'comment' option through to distinct command (#3339) (753ecfe)
- NODE-4413: set maxTimeMS on getMores when maxAwaitTimeMS is specified (#3319) (dcbfd6e)
- NODE-4429: select server sync for endSessions during close (#3363) (5086ead)
- NODE-4467: Add back support for
oplogReplay
option as deprecated (#3337) (6c69b7d) - NODE-4496: counter values incorrectly compared when instance of Long (#3342) (d29eb8c)
- NODE-4513: type for nested objects in query & update (#3349) (ec1a68f)
Documentation
- Reference: https://docs.mongodb.com/drivers/node/current/
- API: https://mongodb.github.io/node-mongodb-native/4.8
- 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.8.1
The MongoDB Node.js team is pleased to announce version 4.8.1 of the mongodb package!
Release Highlights
This patch comes with some bug fixes that are listed below as well as a quality of life improvement for nested keys in the UpdateFilter
and Filter
types. Thanks to @coyotte508 (#3328) for contributing this improvement!
Bug Fixes
- NODE-4423: better type support for nested objects in query & update (#3328) (05e007b)
- NODE-4425: webpack optional import of FLE issue (#3324) (5ab2b05)
- NODE-4444: use Node.js clear timers (#3327) (c5cfe21)
Documentation
- Reference: https://docs.mongodb.com/drivers/node/current/
- API: https://mongodb.github.io/node-mongodb-native/4.8
- 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.8.0
The MongoDB Node.js team is pleased to announce version 4.8.0 of the mongodb package!
Release Highlights
UpdateFilter nested fields
Thanks to a contribution from @coyotte508, in this release you will now get auto-complete and type safety for nested keys in an update filter. See the example below:
Optional client.connect()
fixup
In our last release we made explicitly calling client.connect()
before performing operations optional with some caveats. In this release client.startSession()
can now be called before connecting to MongoDB.
NOTES:
- The only APIs that need the client to be connected before using are the legacy
collection.initializeUnorderedBulkOp()
/collection.initializeOrderedBulkOp()
builder methods. However, the preferredcollection.bulkWrite()
API can be used without calling connect explicitly.- While executing operations without explicitly connecting may be streamlined and convenient, depending on your use case
client.connect()
could still be useful to find out early if there is some easily detectable issue (ex. networking) that prevents you from accessing your database.
Features
- NODE-4078: allow comment with estimated doc count (#3301) (bed1fe3)
- NODE-4267: support nested fields in type completion for UpdateFilter (#3259) (1a9a44c)
Bug Fixes
- NODE-4125: change stream resumability (#3289) (aa5f97e)
- NODE-4262: make startSession work without a connection (#3286) (89ad7c3)
Documentation
- Reference: https://docs.mongodb.com/drivers/node/current/
- API: https://mongodb.github.io/node-mongodb-native/4.8
- 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.7.0
The MongoDB Node.js team is pleased to announce version 4.7.0 of the mongodb package! Happy MongoDB World Day!
Release Highlights
Support for ZSTD Compression
zstd compression is now supported by the NodeJS driver. To enable zstd compression, add it as a dependency in your project: npm install –save @mongodb-js/zstd
. The add the option to your URI options: mongodb://host:port/db?compressors=zstd
.
Improved Connection Storm Avoidance
The Node driver has improved connection storm avoidance by limiting the number of connections that the driver will attempt to open to each server at a time. The number of concurrent connection attempts is set to 2 by default, but can be configured with a new MongoClient argument, maxConnecting
. The following code example creates a new MongoClient that configures maxConnecting
to 5.
const client = new MongoClient('MONGODB_URL', { maxConnecting: 5 });
Expanded Change Stream Events
The collection.watch function now supports a new option, showExpandedEvents
. When showExpandedEvents
is enabled, change streams will report the following events on servers 6.0 and later:
createIndexes
dropIndexes
modify
create
shardCollection
On servers 6.1.0 and later, showExpandedEvents
will also show change stream events for the following commands:
reshardCollection
refineCollectionShardKey
As an example, the following code creates a change stream that has expanded events enabled on a collection:
const client = new MongoClient('MONGODB_URL');
await client.connect();
const collection = client.db('example-db').collection('example-collection');
const changeStream = collection.watch([], { showExpandedEvents: true });
Change Stream Support of Pre/Post Images
Change streams now support pre and post images for update events. To enable pre and post images, the collection must be created with the changeStreamPreAndPostImages
option enabled:
const collection = await db.createCollection(‘collectionName’, { changeStreamPreAndPostImages: { enabled: true }} )
Pre and post images can then be enabled on the change stream when the change stream is created:
const changeStream = collection.watch([], { fullDocumentBeforeChange: ‘required’ })
See the documentation on pre and post images for more information: https://www.mongodb.com/docs/v6.0/changeStreams/#change-streams-with-document-pre--and-post-images.
Improved Performance in Serverless Environments
The driver now only processes the most recent server monitoring event if multiple heartbeat events are recorded in sequence before any can be processed. In serverless environments, this results in increased performance when a function is invoked after a period of inactivity as well as lower resource consumption.
Estimated Document Count uses the Count command
The 5.0 server compatible release unintentionally broke the estimatedDocumentCount
command on views by changing the implementation from the count
command to aggregate
and a collStats
stage. This release fixes estimatedDocumentCount on views by reverting the implementation to use count
.
Due to an oversight, the count command was omitted from the Stable API in server versions 5.0.0 - 5.0.8 and 5.1.0 - 5.3.1, so users of the Stable API with estimatedDocumentCount are recommended to upgrade their MongoDB clusters to 5.0.9 or 5.3.2 (if on Atlas) or set apiStrict: false when constructing their MongoClients.
MongoClient.connect is now optional
If an operation is run before MongoClient.connect is called by the client, the driver will now automatically connect along with that first operation. This makes the repl experience much more streamlined, going right from client construction to your first insert or find. However, MongoClient.connect can still be called manually and remains useful for learning about misconfiguration (auth, server not started, connection string correctness) early in your application's startup.
Note: It's a known limitation that explicit sessions (client.startSession) and
initializeOrderedBulkOp
,initializeUnorderedBulkOp
cannot be used until MongoClient.connect is first called. Look forward to a future patch release that will correct these inconsistencies.
Support for Clustered Collections
Clustered Collections can now be created using the createCollection
method in the Node driver:
const client = new MongoClient('MONGODB_URL');
// No need to connect anymore! (see above)
const collection = await client.db(‘example-db’).createCollection(‘example-collection’, {
key: _id,
unique: true
});
More information about clustered indexes can be found on the official documentation page. https://www.mongodb.com/docs/upcoming/core/clustered-collections/
Automatic Encryption Shared Library
To enable the driver to use the new Automatic Encryption Shared Library instead of using mongocryptd, pass the location of the library in the auto-encryption extra options to the MongoClient
. Example:
const client = new MongoClient(uri, {
autoEncryption: {
keyVaultNamespace: 'encryption.__keyVault',
kmsProviders: {
local: { key: 'localKey' }
},
extraOptions: {
cryptSharedLibPath: "/path/to/mongo_crypt_v1.dylib",
},
encryptedFieldsMap: {
"default.secretCollection": {
[
{
keyId: '_id',
path: 'ssn',
bsonType: 'string',
queries: { queryType: 'equality' }
}
]
},
},
},
})
Queryable Encryption Preview
Queryable Encryption is a beta feature that enables you to encrypt data in your application before you send it over the network to MongoDB while still maintaining the ability to query the encrypted data. With Queryable Encryption enabled, no MongoDB-managed service has access to your data in an unencrypted form.
Checkout the documentation: https://www.mongodb.com/docs/upcoming/core/queryable-encryption/queryable-encryption/
ATTENTION: This feature is included in this release as a beta preview. All related APIs marked with
@expiremental
in the documentation. There are no guarantees that the APIs will not undergo breaking changes without prior notice.
Features:
- NODE-1837: add zstd compression option (#3237) (1261432)
- NODE-2993: implement maxConnecting (#3255) (c9d3816)
- NODE-3750: make maxConnecting configurable (#3261) (ee41447)
- NODE-3938: Add support for pre/post images in change streams (#3250) (981465c)
- NODE-4079: estimated document count uses count (#3244) (a752e75)
- NODE-4081: fix and deprecate change stream resume options (#3270) (47adfb3)
- NODE-4139: streaming protocol message changes (#3256) (4b9ad77)
- NODE-4192: make MongoClient.connect optional (#3232) (a2359e4)
- NODE-4196: add support for showExpandedEvents in change streams (#3254) (9c1782e)
- NODE-4229: bump maxWireVersion to 17 (#3265) (d13cec2)
Bug Fixes
- NODE-4103: respect BSON options when creating change streams (#3247) (b2798d9)
- NODE-4108: improve return type for withTransaction() (#3236) (48e0e6e)
- NODE-4254: allow csfle to be dynamically required (#3260) (cd6b5a0)
- NODE-4281: ensure that the driver always uses Node.js timers (#3275) (4501a1c)
Documentation
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.