0
votes

I wrote a short azure function to simply take data in from CosmosDB change feed Trigger.

However, looks like it returns some form of encoded data.

For example

{
    "_id" : ObjectId("5ad8db107dfa95101430ab94"),
    "id" : "task123",
    "type" : "genera123l",
    "information" : "some task"
}

If above document is created in my cosmos db,

public static void Run(IReadOnlyList<Document> documents, TraceWriter log)
{
    if (documents != null && documents.Count > 0)
    {
        log.Info("Documents modified " + documents.Count);
        log.Info("First document Id " + documents[0].Id);
    }
}

above code will print

2018-04-19T19:28:23.899 [Info] Documents modified 1
2018-04-19T19:28:23.899 [Info] First document Id NWFkOGRiMTA3ZGZhOTUxMDE0MzBhYjk0

instead of

2018-04-19T19:28:23.899 [Info] Documents modified 1
2018-04-19T19:28:23.899 [Info] First document Id task123

which is the expected output. Is there some sort of configuration that I'm missing here?

Looks like none of the documentations for CosmosDB or Azure Function App addresses this issuse :(

Thanks

2
Appears that the Id value is coming from '_id', instead of 'id' parameter of the document. decoding in base64 created correct value for _id. appears that its being read as ObjectId,Evan Park

2 Answers

1
votes

I assume that you use the Azure CosmosDB MongoDb API for input bindings.

Please hava a try to use the Azure CosmosDB SQL API. Detail steps you could refer to this guide. Then you could get the expected answer. For more information, please refer to this link.

Don't use Azure Cosmos DB input or output bindings if you're using MongoDB API on a Cosmos DB account. Data corruption is possible.

enter image description here

0
votes

You are using a MongoDB API account. What you are seeing is the representation of a BSON document in JSON, it is not an encoding issue.

The Change Feed feature is read and accessed using the SQL SDK, this is the reason for the BSON > JSON conversion.

While you can technically use the Change Feed from Functions, using the Change Feed SDK or programmatically using the SQL SDK with any Cosmos DB API account, if you use binary attributes in a Mongo accounts, they will get converted to JSON.