azure-functions - AF 绑定(bind)到 CosmosDb

标签 azure-functions azure-cosmosdb azure-cosmosdb-mongoapi

我正在尝试将数据从 Azure 函数输出到 CosmosDb (MongoDb),我有以下绑定(bind)设置:

[DocumentDB("mydatabase", "mycollection",
    ConnectionStringSetting = "CosmosDBConnection", 
    CreateIfNotExists= true, 
    PartitionKey = "SomeKey")]
IAsyncCollector<MyEntity> mongoBinding,

在我的代码中,我执行以下操作:

var entity = new MyEntity() {SomeKey="X1CLX1010000002", Data = "somedata"};
await mongoBinding.AddAsync(entity);

public class MyEntity {
    public string SomeKey {get; set;}
    public string Data {get; set;}
}

结果错误:

{"Errors":["The partition key component definition path 'SomeKey' could not be accepted, failed near position '0'. Partition key paths must contain only valid characters and not contain a trailing slash or wildcard character."]}

知道我做错了什么吗?

最佳答案

解决方案是在 key 开头添加斜杠,例如 PartitionKey = "/SomeKey"。那么分区键就像一个路径,因此它需要以“/”开头。你没有使用斜杠,所以出现上面的错误。

关于azure-functions - AF 绑定(bind)到 CosmosDb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51520603/

相关文章:

python-3.x - 在 Python Azure 函数中,如何仅用一行代码将共享库导入到我的函数中?

Azure Cosmos DB 与 Firebase firestore 或 firebase 实时数据库相同吗?

stored-procedures - 无法使用数字分区键查询 Cosmos DB

azure - Cosmos Db 需要哪些权限才能访问 Azure 门户中的数据库并查看数据

azure - 如何使用 MongoDB API 将 Azure Databricks 连接到 Cosmos DB?

azure-functions - 从 Azure 函数将消息写入 Azure 服务总线队列

node.js - Azure Function 应用程序 key 经常更改

azure - 使 Azure Functions 不可公开访问吗?

azure-devops - 在 Azure Devops 中将数据播种到 Cosmos DB

node.js - 无法连接到azure cosmos db中的Mongodb