smolesen
smolesen

Reputation: 1333

AF Binding to CosmosDb

I'm trying to output data from an Azure function to CosmosDb (MongoDb), I have the following binding setup:

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

In my code I do the following:

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;}
}

Results in error:

{"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."]}

Any idea to what I'm doing wrong?

Upvotes: 4

Views: 2097

Answers (1)

Pillo
Pillo

Reputation: 185

The solution is to add a slash at the start of your key, like PartitionKey = "/SomeKey". Then partition key acts like a path, thus it needs the "/" at the start. You did not use the slash, so the above error.

Upvotes: 4

Related Questions