java - MongoDB 相当于 Java 中的 SQL 表达式 '1=1'

标签 java sql mongodb aggregation-framework logical-operators

在 SQL 中,您可以执行以下操作;

SELECT * FROM CUSTOMERS WHERE ID=43 AND 1=1

如何将 boolean 表达式(如“1=1”或“4<6”)作为标准提供给 Java 中的 MongoDB 逻辑运算符?

比如,我能做到;

collection.find("$or",Arrays.asList(new Document("field1",value1),new Document("field2",value2)))

然而,上述标准始终基于已经存在的字段,而我想做的更像是这样(不会编译);

collection.find("$and",Arrays.asList(new Document(1,1),new Document("field2",value2)))

我需要这个的原因是因为我有一个“$or”标准列表,但这个列表可能是空的——在这种情况下我根本不想有任何标准。


更新 1

虽然@gil.fernandes 的解决方案非常适合查找 查询,但它不适用于聚合查询(这也是我需要的);

AggregateIterable aggregationQuery = collection.aggregate(Arrays.asList(
    [...]
    new Document("$match", new Document("$or", Arrays.asList(new Document("$where","1==1"))))
));

MongoCommandException: Command failed with error 16395: '$where is not allowed inside of a $match aggregation expression'

关于如何在聚合的 $match 运算符中使用“1=1”逻辑有什么想法吗?

更新 2

我使用 mongo 服务器版本 3.4.7 应用了@Veeram 的第二个解决方案 但是,如果我将 addFieldsmatch 对象包含到我的聚合查询中,我会得到 0 个结果。 如果我删除它们,我会得到所有结果。

collection = mongoClient.getDatabase("testDatabase").getCollection("testColletion");
collection.insertOne(new Document("testField","testValue"));
Bson addFields = Aggregates.addFields(new Field<>("cmp", new Document("$or", Arrays.asList(new Document("$eq", Arrays.asList(1, 1))))));
Bson match = Aggregates.match(Filters.eq("cmp", 1));
AggregateIterable aggregationQuery = collection.aggregate(Arrays.asList(
        new Document("$match", new Document("testField", "testValue")),
        addFields,
        match
));
boolean hasDocuments = aggregationQuery.iterator().hasNext()

最佳答案

您可以 $expr在 3.6 mongo 服务器版本上。

import static com.mongodb.client.model.Aggregates.addFields;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Filters.*;
import static java.util.Arrays.asList;

有点像

Bson match = match(expr(new Document("$or", asList(new Document("$eq", asList(1, 1))))));
AggregateIterable aggregationQuery = collection.aggregate(asList(
                [...]
                match
));

这应该输出类似的查询

{ "$match" : { "$expr" : { "$or" : [{ "$eq" : [1, 1] }] } } }

对于较低的 3.4 版本,您可以使用 $addFields$match 的组合来实现类似的查询。

Bson addFields = addFields(new Field<>("cmp", new Document("$or", asList(new Document("$eq", asList(1, 1))))));
Bson match = match(eq("cmp", true));
AggregateIterable aggregationQuery = collection.aggregate(asList(
               [...]
               addFields,
               match
));

这应该输出类似的查询

{ "$addFields" : { "cmp" : { "$or" : [{ "$eq" : [1, 1] }] } } }    
{ "$match" : { "cmp" : true } }

关于java - MongoDB 相当于 Java 中的 SQL 表达式 '1=1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49730695/

相关文章:

sql - 连接子句中的 Oracle Invalid Number

用于打开存储过程并在 SQL Server 中读取它的 SQL 代码

node.js - mongo如何更新文档

mongodb - 在 Ubuntu Snap 安装上更改默认 MongoDB 端口

java - 带有 Collections$UnmodifiableCollection.add(未知来源)的 Spring RestTemplate UnsupportedOperationException

java - 大九片拉伸(stretch)

java - @Startup注解不起作用

java - 将 EntrySet 的流收集到 LinkedHashMap

javascript - 如何修复 NPM 安装 SQLite 构建失败?

mongodb - 使用 FindWhere 子句时环回 MongoDB 字符串属性转换为 ObjectId