首页 > 解决方案 > CosmosDB mongoDB API 排序和全文搜索抛出错误

问题描述

我正在使用 CosmosDB MongoDB API 来存储和检索集合中的文档。

我正在尝试使用排序 collection.find({}).sort({field1: 1} 并看到此错误:

"message": "Failed to retrieve the document:MongoError: Error=2, Details='Response status code does not indicate success: 
BadRequest (400); Substatus: 0; ActivityId: d05b7772-3252-4005-898f-4480cadfb8e1; Reason: 
(Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 
d05b7772-3252-4005-898f-4480cadfb8e1; Reason: (Response status code does not indicate success: BadRequest (400); 
Substatus: 0; ActivityId: d05b7772-3252-4005-898f-4480cadfb8e1; Reason: (Message: {\"Errors\":
[\"The index path corresponding to the specified order-by item is excluded.\"]}\r\nActivityId: d05b7772-3252-4005-898f-4480cadfb8e1, 
Request URI: /apps/e118124c-80f3-408e-a2f9-fbea51664ad4/services/5cb63213-f046-49d6-9825-0bfa4010772e/partitions
/68271031-2d85-44cd-9929-8857ff69b63e/replicas/132696733163313635s/, 
RequestStats: Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, 
SDK: Windows/10.0.17763 cosmos-netstandard-sdk/3.18.0);););"

这是否意味着没有索引,我们就不能在 MongoDB 上进行排序?

想知道这是 MongoDB 行为还是 CosmosDB 行为?没有把握。

第二个问题是:我想在所有字段中进行字符串搜索,如下所示:

 collection.find({"$text":{"$search":"findMeOut"}});

但是在这里我看到了这个 Mongo 错误:

MongoError: $text not supported

现在这意味着什么?

我正在使用 CosmosDB MongoDB API,使用 JavaScript 和 NodeJs 本机 MongoClient,版本如下:

 "mongodb": "^3.6.10",
 "fastify-mongodb": "^2.0.1",

谢谢。

标签: mongodb-queryazure-cosmosdb-mongoapi

解决方案


分享你的模型真的很有帮助。AFAIK,为了使用文本搜索,您必须使用索引名称text对要执行搜索的字段进行索引。你可以看看这个问题,我相信它与你的问题相似


推荐阅读