You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中MongoDB如何支持SQL中'1=1'这类布尔表达式查询

How to Use Boolean Expressions Like 1=1 or 4<6 in MongoDB Java Driver Queries

Got it, I totally get what you're trying to do—translating those SQL-style constant boolean conditions into MongoDB Java queries can feel tricky at first, since MongoDB's query syntax is field-focused by default. Let's break down how to make this work.

The Key: Use $expr for Aggregation-Based Boolean Checks

MongoDB doesn't natively support raw boolean expressions like 1=1 in standard queries, but the $expr operator lets you use aggregation pipeline expressions directly in your find query. This is exactly what we need to evaluate constant boolean logic.

Let's Walk Through Examples

1. Basic Constant Boolean Condition (e.g., 1=1 or 4<6)

To query with 1=1 (always true), you'd build a Document that uses $expr with an aggregation operator like $eq:

import com.mongodb.client.MongoCollection;
import org.bson.Document;
import java.util.Arrays;

// For 1=1 (always true)
Document trueCondition = new Document("$expr", new Document("$eq", Arrays.asList(1, 1)));
var results = collection.find(trueCondition).iterator();

// For 4<6 (also always true)
Document lessThanCondition = new Document("$expr", new Document("$lt", Arrays.asList(4, 6)));
results = collection.find(lessThanCondition).iterator();

2. Combine with Field Conditions (Like Your Original SQL: ID=43 AND 1=1)

Your original SQL translates to a MongoDB query with $and, combining the ID=43 field filter and the 1=1 boolean expression. Here's how to write this correctly (fixing your earlier compile error):

Option 1: Using Filters helper methods (cleaner, recommended)

import com.mongodb.client.model.Filters;

Document idFilter = Filters.eq("ID", 43);
Document booleanExpr = new Document("$expr", new Document("$eq", Arrays.asList(1, 1)));
// Combine with $and
Document combinedQuery = Filters.and(idFilter, booleanExpr);

// Execute the query
for (Document doc : collection.find(combinedQuery)) {
    System.out.println(doc.toJson());
}

Option 2: Building the $and document manually
If you prefer constructing the Document directly:

Document combinedQuery = new Document("$and", Arrays.asList(
    new Document("ID", 43),
    new Document("$expr", new Document("$eq", Arrays.asList(1, 1)))
));

collection.find(combinedQuery).forEach(doc -> System.out.println(doc.toJson()));

3. More Complex Boolean Logic (e.g., $or with Multiple Boolean Expressions)

You can extend this to any combination of logic operators. For example, if you wanted ID=43 AND (1=1 OR 4<6):

Document orExpr = new Document("$or", Arrays.asList(
    new Document("$expr", new Document("$eq", Arrays.asList(1, 1))),
    new Document("$expr", new Document("$lt", Arrays.asList(4, 6)))
));

Document finalQuery = Filters.and(Filters.eq("ID", 43), orExpr);
collection.find(finalQuery).iterator();

Why Your Original Code Failed

Your initial attempt collection.find("$and", Arrays.asList(...)) doesn't compile because the find() method doesn't accept a string operator as the first parameter directly. You need to wrap the $and logic inside a Document, or use the Filters helper methods to construct valid query objects.


内容的提问来源于stack exchange,提问作者arxakoulini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:12:54