Cosmos Mongodb query fails but azure storage explorer works fine?

荒凉一梦 提交于 2020-01-04 02:16:07

问题


I am trying to query a Cosmos MongoDB collection, I can connect to it fine with Robo3T and 3T Studio, and dotnet core mongo client (in a test harness). I can do a count of entities (db.[collection_name].count({})) in all of the platforms, but every query (db.[collection_name].find({}) fails with the following error :

Error: error: {
"_t" : "OKMongoResponse",
"ok" : 0,
"code" : 1,
"errmsg" : "Unknown server error occurred when processing this request.",
"$err" : "Unknown server error occurred when processing this request."}

Here is my sample query from Rob3T and below that sample .NET harness.. Doesn't matter what I use, same error every time.

db.wihistory.find({})

and the dotnet core code :

string connectionString = @"my connections string here";
        MongoClientSettings settings = MongoClientSettings.FromUrl(
            new MongoUrl(connectionString)
        );
        settings.SslSettings = 
        new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
        var mongoClient = new MongoClient(settings);
        var database = mongoClient.GetDatabase("vstsagileanalytics");
        var collection = database.GetCollection<dynamic>("wihistory");
        var data = collection.Find(new BsonDocument()).ToList();
        System.Console.WriteLine(data.ToString());

回答1:


The issue comes from mixing API usage in the account. As stated in the comments, you are using Azure Function's Cosmos DB Output binding, which uses the SQL API (.NET SDK for SQL API) to connect to the account and store data. There is a note in that documentation that says:

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.

The documents stored through this method do not enforce certain MongoDB requirements (like the existence of a "_id" identifier) that a MongoDB client would (a MongoDB client would automatically create the "_id" if not present).

Robo3T and other Mongo clients (including the Azure Portal) are failing to correctly parse and read the stored documents as valid MongoDB documents (due to the lack of requirements like "_id") and that is the cause of the error.

You can either switch to use a Cosmos DB SQL API account if you want to maintain the Azure Functions pipeline or change the output binding and replace it with a manual implementation of a MongoDB client.



来源:https://stackoverflow.com/questions/48388377/cosmos-mongodb-query-fails-but-azure-storage-explorer-works-fine

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