首页 > 解决方案 > 邮递员:无法使用 Azure MongoDB API 创建文档

问题描述

使用Azure Cosmos DB: MongoDB API创建文档时,我面临以下因参数而异的问题。

创建 collectionUserProfile时,我使用 Partition Key 创建了它username

Request1: - 没有分区键

标题:

x-ms-documentdb-is-upsert: true

身体:

{"username": "test"}

问题:

x-ms-partitionkey 标头中提供的分区键包含的组件少于集合中定义的组件。

Request2: - 使用分区键

标题:

x-ms-documentdb-is-upsert: true
x-ms-documentdb-partitionkey: ["username"]

身体:

{"username": "test"}

问题:

指定的输入之一无效

请求 3: - 在正文中指定了分区键和 id

标题:

x-ms-documentdb-is-upsert: true
x-ms-documentdb-partitionkey: ["username"]

身体:

{"id": "test", "username": "test"}

问题:

从文档中提取的 PartitionKey 与标题中指定的不匹配

任何状况之下,

我无法创建文档。创建指定分区的文档需要哪些参数?

标签: azureazure-cosmosdbazure-cosmosdb-mongoapi

解决方案


两点:

  1. 如果您使用的是 Mongo API,则不应使用 REST API。虽然技术上可行,但不支持同时使用两者。100% 不推荐。

  2. 分区键需要是分区键值,而不是路径本身,所以它将是“测试”,而不是“用户名”。它已经知道“用户名”是路径。


推荐阅读