管理 Azure Cosmos DB 中数据的 Node.js 示例Node.js examples to manage data in Azure Cosmos DB

对 Azure Cosmos DB 资源执行 CRUD 操作和其他常见操作的示例解决方案包含在 azure-cosmos-js GitHub 存储库中。Sample solutions that perform CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-cosmos-js GitHub repository. 本文将提供:This article provides:

  • 指向每个 Node.js 示例项目文件中任务的链接。Links to the tasks in each of the Node.js example project files.
  • 指向相关的 API 参考内容的链接。Links to the related API reference content.

先决条件Prerequisites

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

可以使用 URI 为 https://localhost:8081Azure Cosmos DB 模拟器You can use the Azure Cosmos DB Emulator with a URI of https://localhost:8081. 有关在模拟器中使用的密钥,请参阅对请求进行身份验证For the key to use with the emulator, see Authenticating requests.

也需 JavaScript SDKYou also need the JavaScript SDK.

备注

每个示例都是独立的,自行对自身进行设置并在完成后自行进行清理。Each sample is self-contained, it sets itself up and cleans up after itself. 因此,这些示例对 Containers.create 发出多个调用。As such, the samples issue multiple calls to Containers.create. 每次执行完此操作,均会按照正在创建的容器的性能层,向订阅收取使用 1 小时的费用。Each time this is done your subscription will be billed for 1 hour of usage per the performance tier of the container being created.

数据库示例Database examples

DatabaseManagement 文件演示如何对数据库执行 CRUD 操作。The DatabaseManagement file shows how to perform the CRUD operations on the database. 若要在运行以下示例之前了解 Azure Cosmos 数据库,请参阅使用数据库、容器和项概念性文章。To learn about the Azure Cosmos databases before running the following samples, see Working with databases, containers, and items conceptual article.

任务Task API 参考API reference
创建数据库(如果不存在)Create a database if it does not exist Databases.createIfNotExistsDatabases.createIfNotExists
列出帐户的数据库List databases for an account Databases.readAllDatabases.readAll
按 ID 读取数据库Read a database by ID Database.readDatabase.read
删除数据库Delete a database Database.deleteDatabase.delete

容器示例Container examples

ContainerManagement 文件演示如何对容器执行 CRUD 操作。The ContainerManagement file shows how to perform the CRUD operations on the container. 若要在运行以下示例之前了解 Azure Cosmos 集合,请参阅使用数据库、容器和项概念性文章。To learn about the Azure Cosmos collections before running the following samples, see Working with databases, containers, and items conceptual article.

任务Task API 参考API reference
创建容器(如果不存在)Create a container if it does not exist Containers.createIfNotExistsContainers.createIfNotExists
列出帐户的容器List containers for an account Containers.readAllContainers.readAll
读取容器定义Read a container definition Container.readContainer.read
删除容器Delete a container Container.deleteContainer.delete

项示例Item examples

ItemManagement 文件演示如何对项执行 CRUD 操作。The ItemManagement file shows how to perform the CRUD operations on the item. 若要在运行以下示例之前了解 Azure Cosmos 文档,请参阅使用数据库、容器和项概念性文章。To learn about the Azure Cosmos documents before running the following samples, see Working with databases, containers, and items conceptual article.

任务Task API 参考API reference
创建项Create items Items.createItems.create
读取容器中的所有项Read all items in a container Items.readAllItems.readAll
按 ID 读取项Read an item by ID Item.readItem.read
仅当项更改时读取项Read item only if item has changed Item.readItem.read
RequestOptions.accessConditionRequestOptions.accessCondition
查询文档Query for documents Items.queryItems.query
替换项Replace an item Item.replaceItem.replace
使用条件 ETag 检查替换项Replace item with conditional ETag check Item.replaceItem.replace
RequestOptions.accessConditionRequestOptions.accessCondition
删除项Delete an item Item.deleteItem.delete

索引示例Indexing examples

IndexManagement 文件演示如何管理索引。The IndexManagement file shows how to manage indexing. 若要在运行以下示例之前了解 Azure Cosmos DB 中的索引,请参阅索引策略索引类型索引路径概念文章。To learn about indexing in Azure Cosmos DB before running the following samples, see indexing policies, indexing types, and indexing paths conceptual articles.

任务Task API 参考API reference
手动为特定项编制索引Manually index a specific item RequestOptions.indexingDirective: 'include'RequestOptions.indexingDirective: 'include'
手动从索引中排除特定项Manually exclude a specific item from the index RequestOptions.indexingDirective: 'exclude'RequestOptions.indexingDirective: 'exclude'
从索引中排除路径Exclude a path from the index IndexingPolicy.ExcludedPathIndexingPolicy.ExcludedPath
在字符串路径上创建范围索引Create a range index on a string path IndexKind.RangeIndexingPolicyItems.queryIndexKind.Range, IndexingPolicy, Items.query
使用默认 indexPolicy 创建容器,然后对其进行联机更新Create a container with default indexPolicy, then update this online Containers.createContainers.create

服务器端编程示例Server-side programming examples

ServerSideScripts 项目的 index.ts 文件演示如何执行以下任务。The index.ts file of the ServerSideScripts project shows how to perform the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的服务器端编程,请参阅存储过程、触发器和用户定义函数概念性文章。To learn about Server-side programming in Azure Cosmos DB before running the following samples, see Stored procedures, triggers, and user-defined functions conceptual article.

任务Task API 参考API reference
创建存储过程Create a stored procedure StoredProcedures.createStoredProcedures.create
执行存储过程Execute a stored procedure StoredProcedure.executeStoredProcedure.execute

有关服务器端编程的详细信息,请参阅 Azure Cosmos DB 服务器端编程:存储过程、数据库触发器和 UDFFor more information about server-side programming, see Azure Cosmos DB server-side programming: Stored procedures, database triggers, and UDFs.