v4.3.0
版本发布时间: 2022-01-07 07:54:46
mongodb/node-mongodb-native最新发布版本:v6.9.0(2024-09-13 01:12:41)
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.