Azure Function with Cosmos MongoDB integration not

2019-01-29 00:52发布

问题:

I have setup a Azure Function with a Azure Cosmos DB(document) output. The cosmos database is configured to be a MongoDB. And added the following simple code to try and add a new document:

module.exports = function (context, eventHubMessages) {
   context.bindings.document = {
   text : "Test data"
}   
context.done();
};

When i test run i get success, but when i try to open the the collection using Studio 3T i get:

Query failed with error code 1 and error message 'Unknown server error occurred when processing this request.'

When i use the same code to write to a DocumentDB i get success and i can view data in Azure. Do you need to use a different API to save data to mongoDB?

回答1:

The DocumentDB output binding is using the DocumentDB API to connect and save information in the database. But your database (from what you are saying) is using the MongoDB API, they are different APIs (links point to the docs).

As you surely know, MongoDB has some requirements (like the existence of an "_id" attribute) that are covered when you connect to the database from a MongoDB client (either an SDK or a third-party client), but since you are communicating through the DocumentDB API, it's probably failing to fulfill those requirements.

You might want to try and use the Mongo driver in the function to connect to your Cosmos DB database through the MongoDB API.