的文件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"
}}]);
As Spring Data 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);
The connectFrom和connectTo將字段值(from.refId,to.refId)替換爲refId(「from」和「to」剝離)。因此,查詢不會返回任何結果。 構造函數集合字段類保留原名稱目標字段和集合名稱剝離(「refId」)。 問題是,GraphLookupOperation.toDocument方法使用field.getName()代替field.getTarget():
...
graphLookup.put("connectFromField", connectFrom.getName());
graphLookup.put("connectToField", connectTo.getName());
...
有沒有解決辦法?它將在未來的版本中修復嗎? 據我所知,我可以創建自己的CustomAggregationOperation,但如果有人試圖開發它,那麼使用開箱即用功能將會很不錯。
春數據MongoDB的版本2.0.0.M4