Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring data MongoDB中使用$where?_Java_Spring Boot_Mongodb Query_Where Clause_Spring Data Mongodb - Fatal编程技术网

Java 如何在spring data MongoDB中使用$where?

Java 如何在spring data MongoDB中使用$where?,java,spring-boot,mongodb-query,where-clause,spring-data-mongodb,Java,Spring Boot,Mongodb Query,Where Clause,Spring Data Mongodb,我无法在SpringDataMongoDb中使用$where 有没有办法在SpringBoot中实现这一点 db.getCollection('my_collection').find({ $where : function(){ for (var key in this.action_status){ return this.action_status[key] == false; } }}) 谢谢。它可以通过两种方式实现:一种是在spring boot应用程

我无法在
SpringDataMongoDb
中使用
$where

有没有办法在SpringBoot中实现这一点

db.getCollection('my_collection').find({ $where : function(){
    for (var key in this.action_status){
        return this.action_status[key] == false;
    }
}})

谢谢。

它可以通过两种方式实现:一种是在spring boot应用程序中使用MongoTemplate和条件查询

Query query = new Query();
query.addCriteria(Criteria.where("action_status").eq(false));
List<User> users = mongoTemplate.find(query,MyCollectionClassName.class);
参考资料:

https://www.baeldung.com/queries-in-spring-data-mongodb
https://www.baeldung.com/spring-data-jpa-query

1:您可以创建一个扩展MongoRepository的存储库,并具有一个JSON查询方法,如下所示:

@Query("{'action_status':{$eq:?0}}")
public List<YourObj> findByActionStatus(boolean status);
@Query(“{'action_status':{$eq:?0}”)
公共列表findByActionStatus(布尔状态);
2:您可以在MongoRepository中使用“按对象属性查询”。假设您的对象中有一个名为“isActionStatus”的字段,只需在存储库中声明此方法:

public List<YourObj> findByIsActionStatus(boolean actionStatus);
公共列表findByIsActionStatus(布尔actionStatus);
Spring数据将打破方法名称,如“select*from…”,其中isActionStatus=actionStatus


如果您需要更复杂的查询,您可以在此处找到支持的关键字:

这个问题是关于Spring Data Mongo的,而不是关于Spring Data JPA的
public List<YourObj> findByIsActionStatus(boolean actionStatus);