简体   繁体   English

Mongodb查询以检查参数是否为真且不为null

[英]Mongodb query to check whether a parameter is true and not null

I am using Spring Boot to create an application. 我正在使用Spring Boot创建一个应用程序。 I have created a class for a Data access object which will retrieve data from MongoDB. 我为数据访问对象创建了一个类,该类将从MongoDB中检索数据。

Query: The objective of the query is to check whether the parameter(boolean variable - control) is not null and true 查询:查询的目的是检查参数(布尔变量-控件)是否不为nulltrue

I have tried the following query: 我尝试了以下查询:

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

However, it is incorrect. 但是,这是不正确的。 What should we use for checking whether a variable is not null - $exists or $ne? 我们应该使用什么来检查变量是否不为空-$ exists或$ ne?

You have to check null using if condition, 您必须使用if条件检查null,

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

Sample here here取样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM