我有一个简单的文档细分,其中一个属性名称。每个细分可以包含细分类型的 0..N儿童,他们的孩子也可以有细分儿童0..N,所以基本树结构。
从子段到父级创建边缘。
我需要的是在一个命令中使用所有子孙和孙子等获取NodeJS根目录,所以我可以检索这样的内容:
[
{
"@rid": "#1:1",
"name": "Segment 1",
"segments": [
{
"@rid": "#1:2",
"name": "Segment 1.1",
"segments": [
{
"@rid": "#1:3",
"name": "Segment 1.1.1"
},
{
"@rid": "#1:4",
"name": "Segment 1.1.2"
}
]
},
{
"@rid": "#1:5",
"name": "Segment 1.2",
"segments": [
{
"@rid": "#1:6",
"name": "Segment 1.2.1"
},
{
"@rid": "#1:7",
"name": "Segment 1.2.2"
}
]
}
]
},
{
"@rid": "#1:8",
"name": "Segment 2",
"segments": [
{
"@rid": "#1:9",
"name": "Segment 2.1",
"segments": [
{
"@rid": "#1:10",
"name": "Segment 2.1.1"
},
{
"@rid": "#1:11",
"name": "Segment 2.1.2"
}
]
},
{
"@rid": "#1:12",
"name": "Segment 2.2",
"segments": [
{
"@rid": "#1:13",
"name": "Segment 2.2.1"
},
{
"@rid": "#1:14",
"name": "Segment 2.2.2"
}
]
}
]
}
]
答案 0 :(得分:0)
试试这个:
var OrientDB = require('orientjs');
var db = OrientDB({
host: 'localhost',
port: 2424,
});
var db = db.use({
name: 'your db name',
username: 'your username',
password: 'your password'
});
db.query('traverse * from Segment')
.then(function (response) {
console.log(response);
});
db.close();
希望有所帮助
此致
答案 1 :(得分:0)
function parseSegment(segment) {
var parsedSegment = {
id : segment['@rid'].toString(),
name: segment.name
};
if (segment.in_) {
var children = segment.in_.all().map(edge => parseSegment(edge.out));
if (children.length) {
parsedSegment.children = children;
}
}
return parsedSegment;
}
db.select()
.from('Segment')
.where('out().size() = 0')
.fetch({'in_':'-1'})
.transform(parseSegment)
.all()
.then(segments => {
console.log(JSON.stringify(segments, null, 2));
})
.finally(() => {db.close();});