Cosmos Mongodb查询失败,但是azure Storage Explorer可以正常工作吗? [英] Cosmos Mongodb query fails but azure storage explorer works fine?

查看:86
本文介绍了Cosmos Mongodb查询失败,但是azure Storage Explorer可以正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询Cosmos MongoDB集合,我可以使用Robo3T和3T Studio以及dotnet核心mongo客户端(在测试工具中)很好地连接到它.我可以在所有平台上对实体(db.[collection_name] .count({}))进行计数,但是每个查询(db.[collection_name] .find({})都会失败,并出现以下错误:

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."}

这是我从Rob3T进行的示例查询,在该示例.NET工具下面..不管我使用什么,每次都出现相同的错误.

db.wihistory.find({})

和dotnet核心代码:

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());

解决方案

问题来自帐户中混合使用API​​.如评论中所述,您正在使用 Azure Function的Cosmos DB输出绑定,它使用SQL API(

Robo3T和其他Mongo客户端(包括Azure门户)无法正确地解析和读取存储的文档作为有效的MongoDB文档(由于缺少诸如"_id"之类的要求),这就是错误的原因./p>

如果要维护Azure Functions管道,可以切换为使用Cosmos DB SQL API帐户,也可以更改输出绑定并将其替换为

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());

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.

这篇关于Cosmos Mongodb查询失败,但是azure Storage Explorer可以正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆