2

我想获取由边缘连接的顶点,作为 json 中的属性作为数组返回。

例如:如果 POST 有 10 条评论,则查询应该返回类似这样的内容。

{
    @class: Post,
    postTitle: "Some title",
    comments: [
        { 
           @class: Comment,
           content: "First Comment,
           someKey: "Some Value"  
        },
        { 
           @class: Comment,
           content: "Second Comment
           someKey: "Some Value"
        }
    ]
}

可以通过此查询获取数组中顶点的一个属性

select *, out('HAS_COMMENT').content as comments from POST

这将产生一个数组,该数组在 Comment 类中只有“content”属性的值

我需要以嵌套 json 的形式获取完整记录。

更新

如果我只是out('HAS_COMMENT')在查询中使用而不是out('HAS_COMMENT').content,它将返回@rid字段而不是完整记录。

4

1 回答 1

2

我用这种结构试过你的情况:

create class Post extends V
create class Comment extends V
create class HAS_COMMENT extends E

create property Post.postTitle String
create property Comment.content String
create property Comment.someKey Integer

create vertex Post set postTitle="First"
create vertex Post set postTitle="Second"
create vertex Comment set content="First Comment", someKey="1"
create vertex Comment set content="Second Comment", someKey="2"
create vertex Comment set content="Third Comment", someKey="3"
create vertex Comment set content="Fourth Comment", someKey="4"
create vertex Comment set content="Fifth Comment", someKey="5"
create vertex Comment set content="Sixth Comment", someKey="6"
create vertex Comment set content="Seventh Comment", someKey="7"
create vertex Comment set content="Eighth Comment", someKey="8"
create vertex Comment set content="Ninth Comment", someKey="9"
create vertex Comment set content="Tenth Comment", someKey="10"
create vertex Comment set content="Eleventh Comment", someKey="11"
create vertex Comment set content="Twelfth Comment", someKey="12"
create vertex Comment set content="Thirteenth Comment", someKey="13"
create vertex Comment set content="Fourteenth Comment", someKey="14"

create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="First Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Second Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Third Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Fourth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Fifth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Sixth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Seventh Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Eighth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Ninth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="First") to (select from Comment where content="Tenth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="Second") to (select from Comment where content="Eleventh Comment")
create edge HAS_COMMENT from (select from Post where postTitle="Second") to (select from Comment where content="Twelfth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="Second") to (select from Comment where content="Thirteenth Comment")
create edge HAS_COMMENT from (select from Post where postTitle="Second") to (select from Comment where content="Fourteenth Comment")

要获得您想要的结果,您可以使用以下查询

select expand($ris)
    let $a = (select from Post where postTitle = 'First'),
        $b = (select from Comment where in('HAS_COMMENT').postTitle in $a.postTitle),
        $ris = unionAll($a,$b)

工作室

在此处输入图像描述

控制台输出

----+-----+-------+---------+---------------+---------------+-------+--------------
#   |@RID |@CLASS |postTitle|out_HAS_COMMENT|content        |someKey|in_HAS_COMMENT
----+-----+-------+---------+---------------+---------------+-------+--------------
0   |#12:0|Post   |First    |[size=10]      |null           |null   |null
1   |#13:0|Comment|null     |null           |First Comment  |1      |[size=1]
2   |#13:1|Comment|null     |null           |Second Comment |2      |[size=1]
3   |#13:2|Comment|null     |null           |Third Comment  |3      |[size=1]
4   |#13:3|Comment|null     |null           |Fourth Comment |4      |[size=1]
5   |#13:4|Comment|null     |null           |Fifth Comment  |5      |[size=1]
6   |#13:5|Comment|null     |null           |Sixth Comment  |6      |[size=1]
7   |#13:6|Comment|null     |null           |Seventh Comment|7      |[size=1]
8   |#13:7|Comment|null     |null           |Eighth Comment |8      |[size=1]
9   |#13:8|Comment|null     |null           |Ninth Comment  |9      |[size=1]
10  |#13:9|Comment|null     |null           |Tenth Comment  |10     |[size=1]
----+-----+-------+---------+---------------+---------------+-------+--------------

关于您在UPDATE中下划线的问题,要从@rid您可以使用该expand()功能获取完整记录。

例子:

获取与顶点相关的所有评论 Post wherepostTitle = 'Second'

查询select expand(out('HAS_COMMENT')) from Post where postTitle = 'Second'

工作室

在此处输入图像描述

控制台输出

----+------+-------+------------------+-------+--------------
#   |@RID  |@CLASS |content           |someKey|in_HAS_COMMENT
----+------+-------+------------------+-------+--------------
0   |#13:10|Comment|Eleventh Comment  |11     |[size=1]
1   |#13:11|Comment|Twelfth Comment   |12     |[size=1]
2   |#13:12|Comment|Thirteenth Comment|13     |[size=1]
3   |#13:13|Comment|Fourteenth Comment|14     |[size=1]
----+------+-------+------------------+-------+--------------

希望能帮助到你

已编辑

查询

select *, $a as comments from Post
let $a = (select @class, content, someKey from Comment where in('HAS_COMMENT').postTitle in $parent.current.postTitle)

工作室

在此处输入图像描述

于 2016-01-28T00:15:52.800 回答