OrientDB:如何在 NodeJS 的树结构模型中获取所有文档及其 children(具有所有属性)
OrientDB: How to fetch all documents and their children (with all properties) in a tree structure model in NodeJS
我有一个简单的文档 Segment 和一个 属性 name。每个段都可以有 0..N children 类型的段,它们的 children 也可以有段 children 0..N,如此基本的树结构。
边 是从 child 到 parent 的线段创建的。
我需要的是在一个命令中获取 NodeJS 根 Segements 及其所有 children 和 grandchildren 等等,这样我就可以检索这样的东西:
[
{
"@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"
}
]
}
]
}
]
试试这个:
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();
希望对您有所帮助
此致
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();});
我有一个简单的文档 Segment 和一个 属性 name。每个段都可以有 0..N children 类型的段,它们的 children 也可以有段 children 0..N,如此基本的树结构。
边 是从 child 到 parent 的线段创建的。
我需要的是在一个命令中获取 NodeJS 根 Segements 及其所有 children 和 grandchildren 等等,这样我就可以检索这样的东西:
[
{
"@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"
}
]
}
]
}
]
试试这个:
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();
希望对您有所帮助
此致
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();});