【发布时间】:2020-04-09 10:49:36
【问题描述】:
Newby MongoDB 和 CosmosDB 用户在这里,我已经阅读了这个问题的答案 How does MongoDB treat find().sort() queries with respect to single and compound indexes? 和官方 MongoDB 文档,我相信我的索引创建反映了这个答案,所以我倾向于这是一个 CosmosDB 问题,但阅读他们的文档CosmosDB 3.6 也支持复合索引,所以我现在很茫然。
我可以从 mongo 命令行对一个索引创建为db.Videos.createIndex({"PublishedOn": 1}) 或db.Videos.createIndex({"PublishedOn": -1}) 的集合运行类似db.Videos.find().sort({"PublishedOn": 1}) 的sort() 查询。
当我像 db.Videos.find({"IsPinned": false}).sort({"PublishedOn": 1}) 这样在 find 中添加 'where' 子句时,上述索引仍然有效。
但是我现在有想要避免的文档查找,所以我删除了上面的单字段索引并创建了一个复合索引,如 db.Videos.createIndex({"IsPinned": 1, "PublishedOn": 1}) 或 db.Videos.createIndex({"PublishedOn": 1, "IsPinned": 1}) 但现在查询总是失败并出现错误 The index path corresponding to the specified order-by item is excluded. .
这是 CosmosDB 的限制还是我在索引中的“排序”不好?
【问题讨论】:
-
Cosmos 是一个独立的实现,不与 MongoDB 共享任何底层服务器代码,因此您无法根据 MongoDB 部署支持的内容推断预期行为。例如,错误
The index path corresponding to the specified order-by item is excluded特定于 Cosmos 的 API。我希望{"PublishedOn": 1, "IsPinned": 1}上的索引应该可用(或者更确切地说,MongoDB 根据您的查询使用)。似乎 Cosmos 可能需要不同的索引定义来支持排序。也许索引中的PublishedOn排序顺序必须与查询匹配? -
你说 CosmosDB 不是 MongoDB 是绝对正确的,我希望我没有用我的问题来推断。