2

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?

Imre Pühvel
  • 4,468
  • 1
  • 34
  • 49
smolesen
  • 1,153
  • 3
  • 11
  • 29
  • 1
    Did you try using the curly brackets around your property name? Try `PartitionKey = "{SomeKey}"` – Nick Chapsas Jul 25 '18 at 14:52
  • If I change it to use curly brackets, the I get: Microsoft.Azure.WebJobs.Host: Error indexing method 'BlobStorageEventHandler.Run'. Microsoft.Azure.WebJobs.Host: No binding parameter exists for 'SomeKey'. – smolesen Jul 26 '18 at 04:57
  • `{MyEntity.SomeKey}` ? – Nick Chapsas Jul 26 '18 at 06:44
  • Turns out, that the binding in Azure Functuons doesn't support MongoDb, so I had to switch to the MongoDb driver for .Net (Though using "/SomeKey" did create the collection, but I was unable to get data in there( – smolesen Jul 27 '18 at 14:14

1 Answers1

2

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.

Pillo
  • 165
  • 1
  • 4
  • 11