spring data中mongodb的派生查询方法中如何使用$exists

问题描述 投票:0回答:2

我在仓库中有一个查询,如下所示,


public interface FooRepository extends MongoRepository<Foo, String> {

  Foo findFooById(String id)
}

我需要添加另一个条件来检查字段

$exists
,我已经使用显式
@Query
完成了它 - 但是否可以在查询方法本身中使用它(可能是
findFooByIdAndAnotherFieldExists
或其他东西)?


public interface FooRepository extends MongoRepository<Foo, String> {

  @Query(value="{ id: ?0, anotherField: { $exists: false } }")
  Foo findFooById(String id)

}

java spring-boot spring-data spring-data-mongodb
2个回答
0
投票

当然,这是可能的!您可以在官方 sprign-data-mongdb 文档中找到 MongoDB 中支持查询派生的所有关键字

P.S:所以你的例子(即

findFooByIdAndAnotherFieldExists

)是正确的:)


0
投票
你可以尝试:

公共接口 FooRepository 扩展 MongoRepository

{

Foo findFooByIdAnd(字符串id) }

© www.soinside.com 2019 - 2024. All rights reserved.