具有父引用的MongoDB集合的自我连接(聚合)

时间:2018-07-25 09:10:28

标签: mongodb mongodb-query mongodb-.net-driver mongodb-csharp-2.0

你好,我有这个集合,带有父引用,我需要对该集合进行自我连接,这样输出将在一个文档中包含最大深度的子代,并基于 获得输出”键入“ 字段。

{ 
    "DescId" : "1", 
    "Desc" : "Testing", 
    "ParentId" : "null", 
    "Order" : 1.0, 
    "Type" : "A", 
    "Parent" : null
}

{ 
    "DescId" : "1.1", 
    "Desc" : "Testing Child 1", 
    "ParentId" : "1", 
    "Order" : 1.0, 
    "Type" : "B", 
    "Parent" : "Testing"
}

{ 
    "DescId" : "1.2", 
    "Desc" : "Testing Child 2", 
    "ParentId" : "1", 
    "Order" : 2.0, 
    "Type" : "B", 
    "Parent" : "Testing"
}

{ 

    "DescId" : "1.1.1", 
    "Desc" : "Testing grand child 1", 
    "ParentId" : "1.1", 
    "Order" : 1.0, 
    "Type" : "C", 
    "Parent" : "Testing Child 1"
}

目前,我的输出基于对上一个问题的回答:

{ 
    "ADescId" : "1", 
    "ADesc" : "Testing"
}
{ 
    "BDescId" : "1.1", 
    "BDesc" : "Testing child 1"
}
{ 
    "BDescId" : "1.2", 
    "BDesc" : "Testing Child 2"
}
{ 
    "CDescId" : "1.1.1", 
    "CDesc" : "Testing grandchild 1"
}

我曾尝试在此集合上进行$ graphlookup聚合,但数据似乎不知所措。请让我知道是否有办法实现这一目标。谢谢。

所需的输出:

{ 
    "ADescId" : "1", 
    "ADesc" : "Testing",
    "BDescId" : "1.1", 
    "BDesc" : "Testing child 1",
    "CDescId" : "1.1.1", 
    "CDesc" : "Testing grandchild 1"
}
{ 
    "ADescId" : "1", 
    "ADesc" : "Testing",
    "BDescId" : "1.2", 
    "BDesc" : "Testing Child 2"
}

0 个答案:

没有答案