I am not able to use $where
in SpringDataMongoDb
.
Is there any way to achieve this in SpringBoot
?
db.getCollection('my_collection').find({ $where : function(){
for (var key in this.action_status){
return this.action_status[key] == false;
}
}})
Thank You.
1: You can create a repository that extends MongoRepository and have a JSON Query method as follows:
@Query("{'action_status':{$eq:?0}}")
public List<YourObj> findByActionStatus(boolean status);
2: You can use Query By Object Attribute in MongoRepository. Supposedly you have a field called "isActionStatus" in your object, just declare this method in your repository:
public List<YourObj> findByIsActionStatus(boolean actionStatus);
Spring Data will break the method name like "select * from... where isActionStatus = actionStatus
If you need a more complex query you can find the supported keywords here: https://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mongo.repositories.html
It can be implemented in two ways one is using MongoTemplate and criteria query in spring boot application
Query query = new Query();
query.addCriteria(Criteria.where("action_status").eq(false));
List<User> users = mongoTemplate.find(query,MyCollectionClassName.class);
Another way is using mongo repository
@Query("SELECT mc FROM my_collection mc WHERE mc.status = false")
MyCollectionClassName findMyCollectionClassNameByStatus(Integer status);
References:
https://www.baeldung.com/queries-in-spring-data-mongodb
https://www.baeldung.com/spring-data-jpa-query
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.