【问题标题】:Mongo Go Driver Count() method gets "Invalid response from server, value field is not a number" from Azure CosmosDB MongoDB Api Count()Mongo Go Driver Count() 方法从 Azure CosmosDB MongoDB Api Count() 获取“来自服务器的无效响应,值字段不是数字”
【发布时间】:2019-07-05 08:38:55
【问题描述】:

我在我的项目中使用 Azure Cosmos DB 和 MongoDB Api,该项目是使用 Golang 和 MongoDB Go 驱动程序开发的。当我尝试使用以下代码进行计数时,会出现错误Invalid response from server, value field is not a number

itemCount, err := myCollection.CountDocuments(ctx, bson.D{{"$and", bson.A{bson.M{"userid": user.ID, "siteid": site.ID}}}})

当我对 MongoDB 数据库进行尝试时,相同的代码会返回项目数(在本例中为 532)。 然后我决定通过编写一个简单的 .Net 应用程序来尝试相同的查询,该应用程序在 MongoDB 和 CosmosDB 上都运行良好。下面是我使用的代码,它为两个数据库打印了532

var count = database.GetCollection<Model>("myCollection").CountDocuments(filter);
Console.WriteLine(count);

我还尝试通过 Robo3T 查询这两个数据库。查询 db.getCollection('Consents').count({}) 从 MongoDB 返回 532,而相同的查询从 CosmosDB 返回 NumberLong(532)

我将此行为写到 MSDN 论坛,但没有人相信。我什至不确定这是数据库 api 还是驱动程序的问题。如果是驱动程序,它可以与 MongoDB 一起使用,这就是它向我承诺的。在数据库 api 的情况下,它与 .Net 驱动程序一起使用,它告诉我微软很清楚这种行为,并认为它可以接受并在其驱动程序中处理它。有人知道如何克服这种情况吗?

【问题讨论】:

  • CosmosDB 不是 MongoDB,由于兼容性不匹配,您不应该期望它应该可以工作。说了这么多,你用的是哪个版本的 MongoDB Go 驱动和 MongoDB .NET 驱动呢?
  • 我知道 CosmosDB 与 MongoDB 不是 100% 兼容,但 GO 驱动程序和 .Net 驱动程序之间存在不一致,这两个驱动程序都是由 MongoDB 开发的,并且处理 CosmosDB 的非标准返回和其他并没有让我感到奇怪。顺便说一句,我正在使用 MongoDB Go Driver v1.0.3
  • Go 驱动程序是否有更多错误堆栈跟踪?我找不到您在驱动程序本身中发布的错误行。另外Microsoft is well aware of this behavior and finds it acceptable and handles it within its drivers 是什么意思?您使用的是 Microsoft .NET 驱动程序吗?

标签: mongodb go azure-cosmosdb azure-cosmosdb-mongoapi


【解决方案1】:

你的过滤器应该是:

itemCount, err := myCollection.CountDocuments(ctx, bson.M{"userid": user.ID, "siteid": site.ID})

bson.M 将执行隐式 $and

【讨论】:

  • 谢谢,这有助于我简化代码,但即使没有过滤器,两个数据库也会对查询给出不同的响应,这似乎是问题所在。
猜你喜欢
  • 2020-05-01
  • 2022-12-22
  • 2013-02-02
  • 1970-01-01
  • 2019-04-06
  • 2020-11-07
相关资源
最近更新 更多