当前位置: 首页 > 知识库问答 >
问题:

无法在Spring Data MongoDB中使用查询提示

巫马泰
2023-03-14

我有一个包含以下格式的文档的集合:

{
    "_id" : "id1",
    "closedAt" : ISODate("2019-04-01T15:19:34.189Z"),
    "attempts" : [{
            "successful" : false
        }],
    "version" : 1,
}

其中,closedAt是可选的,可能不存在。我还有一个定义如下的索引:

{
    "v" : 2,
    "key" : {
        "closedAt" : 1,
        "attempts.successful" : 1
    },
    "name" : "closed_at_and_attempts_successful_idx",
    "ns" : "x",
    "sparse" : true
}

我可以要求mongo返回没有关闭的字段并且只有成功false的尝试的文档,并提示使用closed_at_and_attempts_successful_idx索引:

db.x.find({"$and": [{ "closedAt" : {"$exists":false}}, {"attempts.successful" : {"$all" : [false]}}]}).hint("closed_at_and_attempts_successful_idx")

这将在MobgoDB控制台中返回正确的结果。然而,我使用的是Spring Boot 2.1和Spring Data MongoDB(反应式),我无法将其转换为Spring中可用的。我尝试了很多方法,例如:

val document = Document.parse("{'\$and' : [{'attempts.successful' : {'\$eq' : false}},{'\$nor' : [{'closedAt' : {'\$exists' : true}}]}]}")
val query = BasicQuery(document).withHint("{ 'closed_at_and_attempts_successful_idx' : 1 }")
return mongoReactiveOperations.find(query, X::class.java). ..

val q: Bson = and(Arrays.asList(exists("closedAt", false), all("chargingAttempts.successful", Arrays.asList(false))))
return mongo.getCollection("x").find(q).hint("closed_at_and_attempts_successful_idx").toFlux().map { source ->
    mongo.converter.read(X::class.java, source)
}. ..

但我所有的尝试都失败了:

com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'error processing query: ns=order.order batchSize=256Tree: $and
    attempts.successful $eq false
    $not
        closedAt exists
Sort: {}
Proj: {}
 planner returned error: bad hint' on server localhost:27017

如果我删除查询提示,它似乎可以工作。

问:如何在Spring Data MongoDB中使用指定的提示执行查询?

共有1个答案

魏高邈
2023-03-14

您可以这样使用:

Query query = new Query();
query.withHint("lastUpdateDate_-1");
mongoTemplate.find(query, SomeClass.class);

方法withHint的参数是索引名;

索引名应该是数据库中的索引名,而不是列名,而不是编写时的json字符串;

 类似资料:
  • 问题是我得到错误(过滤器表达式只能包含非主键属性:主键属性:名称)。租户是我的主分区键,名称是我的主排序键。 我需要在dynamo db中编写与此等效的内容:从项目中选择*,其中tenant='testProject',name in('John','Dave')。

  • 当我不向其添加某些脚本但它失败并返回时,函数脚本查询工作正常 工作查询: 查询返回错误的次数最多: 任何形式的帮助都是非常感激的。 [在此处输入图像描述][1] 谢谢// [1]:https://i.stack.imgur.com/8LMRj.png**strong文本**

  • 我试图使用solrj构建一个solr查询。根据我对Solr-7.5.0的理解,solrj的所有库和依赖项都应该包含在我的Solr安装中。下面是我的/dist文件夹,后面是我的/dist/solrj-lib文件夹 现在,我的查询将被绑定到一个html post表单,但我想让solrj先工作。这是我全部的solrj 这将无法编译,因为它无法识别我的类(SolrClient、SolrQuery等)。我肯

  • 我的ParkingLotEntity: 我的Jpa存储库接口 上面的两个方法运行良好,但问题是我还需要从结果集到第二个方法“GetAllParkingLotCurrentlyWorkingInRegionOfRadius”中的位置的任何停车场之间的距离!在SQL Server中,我的定义函数已经计算出了距离。方法2的结果集是成功的,但没有返回距离(这是我的需要),只是一组parkingLotEnt

  • jpaQuery.from(tableA,tableb)。(如何编写以下条件)。id=表格b。id() 如何使用查询dsl编写左向外连接?? 这是编写eq连接的示例代码 JPA query query = new JPA query(em); Q表A = Q表A.QTableB 表 B = QTableB 表 B query.from(tableA, tableB). where(tableA.i

  • 问题内容: 我正在尝试使用mybatis运行一个简单的sql查询,但是它给了我以下异常 我的UserMapper.xml是 我的UserMapper是 我试图在我的LoginController中访问它 我的spring-servlet.xml文件是 我不知道为什么会出现此错误。 问题答案: 看看错误 看来myBatis找不到您的查询。那可能是因为找不到您的xml映射。它应该符合您的配置: 在项目