TypeScript

In this guide, you can learn about the TypeScript features and limitations of the MongoDB Node.js driver. 在本指南中,您可以了解MongoDB Node.js驱动程序的TypeScript功能和限制。TypeScript is a strongly typed programming language that compiles to JavaScript.TypeScript是一种编译为JavaScript的强类型编程语言。

All TypeScript features of the driver are optional. 驱动程序的所有TypeScript功能都是可选的。All valid JavaScript code written with the driver is also valid TypeScript code.使用驱动程序编写的所有有效JavaScript代码也是有效的TypeScript代码。

For more information, see the TypeScript website.有关更多信息,请参阅TypeScript网站

If you use TypeScript, you can specify a type for some classes in the driver. 如果使用TypeScript,可以为驱动程序中的某些类指定类型。All classes that accept a type parameter in the driver have the default type Document. 在驱动程序中接受类型参数的所有类都具有默认类型DocumentThe Document interface has the following definition:Document接口具有以下定义:

interface Document {
  [key: string]: any;
}

Any object type can extend the Document interface.任何对象类型都可以扩展Document接口。

For more information on object types, see the TypeScript handbook.有关对象类型的详细信息,请参阅TypeScript手册

The following classes accept any type that extends the Document interface:以下类接受扩展Document接口的任何类型:

You can pass a type parameter that extends the Document interface like this:您可以传递一个扩展Document接口的类型参数,如下所示:

interface Pet {
  name: string;
  age: number;
  cute: true;
 }
const database = client.db("<your database>"); const collection = database.collection<Pet>("<your collection>");
Important
Keys Not in Type Parameter Receive any Type不在类型参数中的键接收任何类型

Keys not listed in your specified type parameter receive the any type. 未列在指定类型参数中的键将接收any类型的。The following code snippet demonstrates this behavior:以下代码段演示了此行为:

interface TestNumber {
  myNumber: number;
 }
const database = client.db("<your db>"); const collection = db.collection<TestNumber>("..."); collection.find({ someRandomKey: "Accepts any type!" });

The following classes accept any type parameter:以下类接受任何类型参数:

You can find a code snippet that shows how to specify a type for the FindCursor class in the Find Multiple Documents Usage Example.您可以在查找多个文档用法示例中找到一个代码段,该代码段显示了如何为FindCursor类指定类型。

The driver cannot infer the type of values with keys containing dot notation. 驱动程序无法使用包含点符号的键推断值的类型。Dot notation is a property access syntax for navigating BSON objects. 点表示法是用于导航BSON对象的属性访问语法。Click on the tabs to see code snippets that highlight this behavior:单击选项卡以查看突出显示此行为的代码段:

The following code snippet does not raise a type error:以下代码段不会引发类型错误:

interface TestType {
  field: { nested: number };
}
const database = client.db("<your db>");
const collection = database.collection<TestType>("<your collection>");
await collection.updateOne({}, { $set: { "field.nested": "A string" } });

The following code snippet raises a type error:以下代码段引发类型错误:

interface TestType {
  field: { nested: number };
}
const database = client.db("<your db>");
const collection = database.collection<TestType>("<your collection>");
await collection.updateOne({}, { $set: { field: { nested: "A string" } } });

This is the error:这就是错误:

Type 'string' is not assignable to type 'number'.

Despite the lack of type safety, we still recommend that you use dot notation to access nested fields in query and update documents when you use TypeScript. 尽管我们建议您在查询时仍使用“点”和“点”安全表示法来更新嵌套类型的文档。You must manually check that your nested field values have your intended type.必须手动检查嵌套字段值是否具有预期类型。

Note
Reason To Use Dot Notation使用点符号的原因

In the MongoDB Query Language, you must match a subdocument exactly when specifying subdocuments in a query. 在MongoDB查询语言中,在查询中指定子文档时,必须精确匹配子文档。Dot notation allows you to query nested fields without matching subdocuments exactly.点表示法允许您查询嵌套字段,而无需精确匹配子文档。

To show this behavior, lets say you have a collection containing only the following document:要显示此行为,假设您有一个仅包含以下文档的集合:

{ field: { s1: "hi", s2: "bye" } }

The following query returns no results from this collection, as the value of field does not exactly match { s1: "hi" }:以下查询不会从此集合返回任何结果,因为field的值与{ s1: "hi" }不完全匹配:

// returns no documents
collection.find({ field: { s1: "hi" } });

The following queries both return your document:以下查询都会返回您的文档:

// returns your document (uses dot notation)返回您的文档(使用点符号)
collection.find({ "field.s1": "hi" });
// returns your document (does not use dot notation)返回您的文档(不使用点符号) collection.find({ $jsonSchema: { required: ["field"], properties: { field: { bsonType: "object", properties: { s1: { enum: ["hi"] } } }, }, }, });

The syntax of the query that does not use dot notation is cumbersome and hard to understand, and may not be worth the type safety obtained from avoiding dot notation.不使用点表示法的查询语法既麻烦又难以理解,可能不值得通过避免点表示法获得类型安全性。

For more information on dot notation, see the MongoDB Manual.有关点表示法的更多信息,请参阅MongoDB手册