如何使用父ID添加父名称及其名称

时间:2018-02-02 03:41:43

标签: node.js mongodb loopback

我有以下数组保存在数据库中。我想修改它以显示如下所示,在localeName键中显示其父级的层次结构。

var allLocales = [
{
    id: 123,
    localeName: 'Test',
    parentId: null
},
{
    id: 456,
    localeName: 'Test 1',
    parentId: 123
},
{
    id: 789,
    localeName: 'Test 2',
    parentId: 456
}
]

我想通过使用父母改变他们的显示名称来将上面的数组更改为以下数组。:

allLocales = [
{
    id: 123,
    localeName: 'Test',
    parentId: null
},
{
    id: 456,
    localeName: 'Test > Test 1',
    parentId: 123
},
{
    id: 789,
    localeName: 'Test > Test 1 > Test 2',
    parentId: 456
}
]

2 个答案:

答案 0 :(得分:1)

您需要使用递归函数来解决此问题。 我做了下面的测试。 请参阅功能。

var allLocales = [
  {  id: 123, localeName: 'Test', parentId: null },
  {  id: 456, localeName: 'Test 1',  parentId: 123 },
  {  id: 789, localeName: 'Test 2', parentId: 456 }
];


function nameRecursion(element) {
    if(element.parentId == null) {
        return element.localeName
    }else {
        var parent = allLocales.find(item => item.id === element.parentId);
        return nameRecursion(parent) + " -> " + element.localeName;
    }
}

var newArray = allLocales.map(a => Object.assign({}, a));
for(var i=0; i<allLocales.length; i++){
     newArray[i].localeName = nameRecursion(allLocales[i]);
}

console.log(allLocales);
console.log(newArray);

答案 1 :(得分:0)

如果您使用mongo 3.4 +

,请尝试此聚合

您可以使用$graphLookup进行分层查询$graphLookup

db.locales.aggregate(
    [
        {$graphLookup : {
            from : "locales",
            startWith : "$parentId",
            connectFromField : "parentId",
            connectToField : "id",
            as : "parents"
            }
        },
        {$addFields : {localeName : {$substr : [{$concat : [{$reduce : {input : "$parents", initialValue : "", in : {$concat : ["$$value", " > ", "$$this.localeName"]}}}, " > " ,"$localeName"] }, 3 , 1000]}}},
        {$project : {parents : 0}}
    ]
).pretty()

集合

> db.locales.find()
{ "_id" : ObjectId("5a73dead0cfc59674782913a"), "id" : 123, "localeName" : "Test", "parentId" : null }
{ "_id" : ObjectId("5a73dead0cfc59674782913b"), "id" : 456, "localeName" : "Test 1", "parentId" : 123 }
{ "_id" : ObjectId("5a73dead0cfc59674782913c"), "id" : 789, "localeName" : "Test 2", "parentId" : 456 }
> 

结果

> db.locales.aggregate( [ {$graphLookup : { from : "locales", startWith : "$parentId", connectFromField : "parentId", connectToField : "id", as : "parents" } }, {$addFields : {localeName : {$substr : [{$concat : [{$reduce : {input : "$parents", initialValue : "", in : {$concat : ["$$value", " > ", "$$this.localeName"]}}}, " > " ,"$localeName"] }, 3 , 100]}}}, {$project : {parents : 0}} ] ).pretty()
{
    "_id" : ObjectId("5a73dead0cfc59674782913a"),
    "id" : 123,
    "localeName" : "Test",
    "parentId" : null
}
{
    "_id" : ObjectId("5a73dead0cfc59674782913b"),
    "id" : 456,
    "localeName" : "Test > Test 1",
    "parentId" : 123
}
{
    "_id" : ObjectId("5a73dead0cfc59674782913c"),
    "id" : 789,
    "localeName" : "Test > Test 1 > Test 2",
    "parentId" : 456
}