我如何在春季数据mongodb中使用$ where?

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

我无法使用$where中的SpringDataMongoDb

SpringBoot中有什么方法可以实现?

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

谢谢。

java spring-boot mongodb-query where-clause spring-data-mongodb
1个回答
0
投票

有两种实现方式,一种是在春季启动应用程序中使用MongoTemplate和条件查询

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

另一种方法是使用mongo仓库

@Query("SELECT mc FROM my_collection mc WHERE mc.status = false")
MyCollectionClassName findMyCollectionClassNameByStatus(Integer status);

参考:

https://www.baeldung.com/queries-in-spring-data-mongodb
https://www.baeldung.com/spring-data-jpa-query
© www.soinside.com 2019 - 2024. All rights reserved.