【问题标题】:Mongodb query to check whether a parameter is true and not nullMongodb查询以检查参数是否为真且不为空
【发布时间】:2018-07-15 04:28:23
【问题描述】:

我正在使用 Spring Boot 创建一个应用程序。我为数据访问对象创建了一个类,该对象将从 MongoDB 中检索数据。

查询:查询的目的是检查参数(布尔变量-控件)是否不为空true

我尝试了以下查询:

    Query query = new Query();
    query.addCriteria(new Criteria().andOperator(
    Criteria.where({"control":{$ne:null}}),
Criteria.where("control").is(true)));

但是,这是不正确的。我们应该使用什么来检查变量是否不为空 - $exists 或 $ne?

【问题讨论】:

    标签: java mongodb spring-boot dao mongotemplate


    【解决方案1】:

    你必须使用 if 条件检查 null,

    if(null != control) {
                query.addCriteria(Criteria.where("status").is(control));
    }
    

    示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2011-04-05
      • 2019-01-03
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多