java - 将具有多个 $expr 的 mongodb 查找写入 java
问题描述
我有以下查找脚本:-
{
"$lookup": {
"from": "table2",
"let": {"s_id": "$s_id","s_date": "$s_date"},
"pipeline": [{
"$match": {
"$expr": {"$eq": ["$$s_id","$s_id"]},
"$expr": {"$eq": ["$$s_date","$s_date"]}
}
}
],
"as": "table2_values"
}
}
$expr
值将是动态的(在 1 个字段或超过 1 个字段上),但这里我以 2 个字段为例。我正在尝试将上面的聚合脚本写入 java。请你帮助我好吗
List<String> whereClauseTable1List = new ArrayList<>();
whereClauseTable1List.add("s_id");
whereClauseTable1List.add("s_date");
List<String> whereClauseTable2List = new ArrayList<>();
whereClauseTable2List.add("s_id");
whereClauseTable2List.add("s_date");
BasicDBObject letTable1Var = new BasicDBObject();
for(String whereClauseTable1: whereClauseTable1List){
letTable1Var.put(whereClauseTable1,"$"+whereClauseTable1);
}
BasicDBObject pipeLineMatchExp = new BasicDBObject();
int index = 0;
for(String whereClauseTable2: whereClauseTable2List){
pipeLineMatchExp.append("$expr", new BasicDBObject("$eq", new
Object[]{"$$" + whereClauseTable2, "$" +
whereClauseTable1List.get(index)}));
index++;
}
DBObject lookupDBOp = new BasicDBObject(
"$lookup",
new BasicDBObject("from","table2")
.append("let", letTable1Var)
.append("pipeline",new Object[]{new
BasicDBObject("$match", pipeLineMatchExp)})
.append("as", "table2_values")
);
实际上BasicObject
不带重复键,所以它总是有 1 个$expr
条件。我想写上面的脚本
解决方案
推荐阅读
- laravel - 将来自不同表的数据合并到一个集合中
- java - Java 8 可选 - 我应该在这个例子中使用它吗
- java - 我无法访问类的方法
- python - 如何在二维列表中找到两个特定条件?
- oauth-2.0 - 客户端凭据流发送 401 错误,而单个流正在使用 Microsoft Graph API
- c# - .Net Core 警告未配置 XML 加密器
- ios - 单个购物车文件以指定和安装多个目标的依赖项
- regex - 如何在替换的正则表达式部分将变量插入到正则表达式模式中?
- python - 使用 smtplib 接收的邮件中不包含主题
- java - Dynamo DB 对 HasKey 列表运行多个查询,但没有范围键