集合test_links中的文档如下:
{
"_id" : "57:58",
"from" : {
"orgunitType" : "Regional",
"refId" : "57",
"name" : "Root Node"
},
"to" : {
"orgunitType" : "Department",
"refId" : "58",
"name" : "Department1"
},
"active" : true,
}在尝试实现MongoDB聚合查询时:
db.test_links.aggregate([
{$match: {"to.refId":"64"}},
{$graphLookup:{
from: "test_links",
startWith: "$to.refId",
connectFromField: "from.refId",
connectToField: "to.refId",
as: "parents"
}}]);作为Spring数据,MongoDB:
Aggregation agg = Aggregation.newAggregation(match(where("to.refId").is(id)),
graphLookup("test_links").startWith("$to.refId").connectFrom("from.refId")
.connectTo("to.refId").as("parent")
);
AggregationResults results = infraTemplate.aggregate(agg, "test_links", Map.class);connectFrom和connectTo字段值(from.refId,to.refId)替换为refId ("from“和"to”)。因此,查询不返回任何结果。AggregationField类的构造函数将原始名称保留在目标字段中,并将名称去掉("refId")。问题是GraphLookupOperation.toDocument方法使用field.getName()而不是field.getTarget()
...
graphLookup.put("connectFromField", connectFrom.getName());
graphLookup.put("connectToField", connectTo.getName());
...有解决办法吗?它会在将来的版本中被修复吗?我知道我可以创建我自己的CustomAggregationOperation,,但是如果有人试图开发它,那么使用开箱即用的特性将是很好的。
Spring数据MongoDB版本2.0.0.M4
发布于 2018-01-16 12:34:35
在spring-boot-starter-data-mongodb\1.5.4.RELEASE中同样的行为,不确定是否会如此,但我找到了解决办法。
connectToField: "to.refId"的问题是,这里使用了嵌入式值,通过提供到该字段的完整路径,一切正常工作,就像预期的那样,因此应该改为:connectToField: "COLLECTION_NAME.to.refId"。
https://stackoverflow.com/questions/45424401
复制相似问题