【问题标题】:Cosmos DB findOneAndUpdate: Sort on multiple parameters fails with unexpected errorCosmos DB findOneAndUpdate:对多个参数进行排序失败并出现意外错误
【发布时间】:2019-01-31 12:58:46
【问题描述】:
  1. 我设置了一个公开 MongoDB API 的 Azure Cosmos DB。
  2. 我使用 mongo shell 连接到数据库。
  3. 我用use sample 创建了一个新数据库sample
  4. 我用db.sample.insert({}) 创建了一个简单的集合sample
  5. 然后我尝试运行以下查询:
db.sample.findOneAndUpdate({ foo: {"$eq":1}},{ "$set": { "bar":1}},{"sort": { "foo": 1, "bar": 1 }})

当我对一个普通的 MongoDB 3.2.0 实例运行它时,查询成功完成(并匹配 0 个结果)。但是,在 CosmosDB 上,会生成以下堆栈跟踪:

2019-01-31T13:46:49.740+0100 E QUERY    [thread1] Error: findAndModifyFailed failed: {
    "_t" : "OKMongoResponse",
    "ok" : 0,
    "code" : 2,
    "errmsg" : "Message: {\"Errors\":[\"Encountered exception while executing function. Exception = Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\r\\nStack trace: Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\n   at queryCallback (__.sys.commonUpdate_BsonSchema.js:3835:26)\\n   at Anonymous function (__.sys.commonUpdate_BsonSchema.js:607:29)\"]}\r\nActivityId: ff7b4476-0000-0000-0000-000000000000, Request URI: /apps/c2de8f1c-dee1-4b6c-8543-479a695c3bb2/services/12da6069-653a-45be-bfa1-4394a0798877/partitions/2814e64e-3d06-4705-a346-cbbfc822a49a/replicas/131934057017796979p/, RequestStats: \r\nRequestStartTime: 2019-01-31T12:46:49.6846466Z, Number of regions attempted: 1\r\n, SDK: Microsoft.Azure.Documents.Common/2.1.0.0",
    "$err" : "Message: {\"Errors\":[\"Encountered exception while executing function. Exception = Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\r\\nStack trace: Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\n   at queryCallback (__.sys.commonUpdate_BsonSchema.js:3835:26)\\n   at Anonymous function (__.sys.commonUpdate_BsonSchema.js:607:29)\"]}\r\nActivityId: ff7b4476-0000-0000-0000-000000000000, Request URI: /apps/c2de8f1c-dee1-4b6c-8543-479a695c3bb2/services/12da6069-653a-45be-bfa1-4394a0798877/partitions/2814e64e-3d06-4705-a346-cbbfc822a49a/replicas/131934057017796979p/, RequestStats: \r\nRequestStartTime: 2019-01-31T12:46:49.6846466Z, Number of regions attempted: 1\r\n, SDK: Microsoft.Azure.Documents.Common/2.1.0.0"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:757:1
DBCollection.prototype.findOneAndUpdate@src/mongo/shell/crud_api.js:785:12
@(shell):1:1

CosmosDB 中是否存在一些在普通 MongoDB 实例中不存在的额外查询限制?这只是一个 CosmosDB 错误吗?报告此类错误的适当方法是什么?

【问题讨论】:

  • 如果没有sort,您的findOneAndUpdate() 通话是否有效?还是使用单一属性sort?目前不支持复合索引(您可以对 Uservoice 请求进行投票,here)。
  • @DavidMakogon 它在没有排序和单一属性排序的情况下工作。我认为您已经正确识别了 Azure 限制。如果您将您的评论放在 SO 答案中,我会认为它是正确的。

标签: azure-cosmosdb azure-cosmosdb-mongoapi


【解决方案1】:

您没有看到错误;目前 Cosmos DB 的 MongoDB API 不支持复合索引。您的findOneAndUpdate() 应该可以在没有sort 或单一属性sort 的情况下找到。

注意:发布了一个 UserVoice 请求 (here),您可以对其进行投票,并且产品团队提供了状态更新;这可能对你有帮助。

【讨论】:

  • 两个后续问题: 1. 有没有官方的 Micorsoft 文档描述了这个限制? 2. 对于任何包含多个字段的排序语句,是否会遇到此限制?
  • 团队有一个已发布的兼容性文档 (here)。我没有看到提到 sort 限制,但请随时为它做出贡献(该页面支持通过 github 进行编辑)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 2021-09-30
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
相关资源
最近更新 更多