java - 如何使用 Spring Data MongoDB 1.8.2 进行“加入”查找?
问题描述
我需要在两个不同的表之间进行“连接”,我的查询是这样的:
db.Summary.aggregate([
{
$lookup: {
from: "OriginFilter",
localField: "origin",
foreignField: "code",
as: "originObject"
}
},
{
$project: {
"hub": 1,
"moment": 1,
"origin": { $arrayElemAt: [ "$originObject.displayName", 0 ] },
"_id": 0
}
}
]);
并且工作完美,但我不知道如何在代码中使用 java 和 spring 来做到这一点。
我有 LookupAggregationOperation 类:
public class LookupAggregationOperation implements AggregationOperation {
private final DBObject operation;
public LookupAggregationOperation(String from, String localField,
String foreignField, String as) {
this.operation = new BasicDBObject("$lookup",
new BasicDBObject("from", from)
.append("localField", localField)
.append("foreignField", foreignField)
.append("as", as));
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
我以这种方式使用它:
LookupAggregationOperation getOriginDisplayName = new LookupAggregationOperation("OriginFilter","origin","code","originObject");
然后,我有一个 groupOperation:
GroupOperation groupByDay = group("moment.year", "moment.month", "moment.day", "moment.hour", "countType", "origin", "originObject")
.sum("$count").as("count");
最后我有一个 ProjectOperation:
ProjectionOperation renameFields = project("count", "countType")
.and("_id.originObject.displayName").as("originDisplayName").and("_id.year").as("moment.year")
.and("_id.month").as("moment.month")
.and("_id.day").as("moment.day")
.and("_id.hour").as("moment.hour");
但and("_id.originObject.displayName").as("originDisplayName")
不工作。如何进行上述查询?
解决方案
推荐阅读
- python-3.x - 数据类型为字典时如何使用循环
- php - 仅更改一个特定 PHP 脚本的上传大小
- qt - 'operator==' 不匹配(操作数类型为 QSerialPortInfo 和 const QSerialPortInfo)
- ios - 如何在自定义 CollectionView 中使用 UIScrollViewDelegate?
- node.js - 用于节点 js 应用程序的 aws cloudwatch 中的 API 日志
- angular - 从其他组件使用时如何在角度模板中替换注入的服务
- httpd.conf - 我的服务器在 /var/log/httpd 上不断生成以下错误。任何 apache 专家都可以在这方面帮助我吗?
- ios - iOS 13:空间分组分隔符已更改
- here-api - 波兰地图和路由引擎缺少 A1 高速公路
- node.js - 在目录上使用 fs.watch 似乎不会注意到添加的文件