Azure Function CosmosDB Trigger returned encoded data?

南楼画角 提交于 2019-12-13 20:19:55

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/49928894/azure-function-cosmosdb-trigger-returned-encoded-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!