我如何编写查询以获得以下结果?

时间:2016-06-25 00:46:35

标签: orientdb orientdb-2.1

我有两节课。

活动和评论。

活动与评论相关联。

评论,有字段username,comment和created_at

当我执行select from Activities时,这是我当前结果的一个示例。下面,您将看到我想要的结果示例(示例2)

示例1

{
            "@type": "d",
            "@rid": "#28:38",
            "@version": 10,
            "@class": "Activities",
            "activity_type_id": 1,
            "username": "johnt",
            "title": "random new activitie test",
            "image": "http://www.someurl.com/images/blah-2.png",
            "comments": [
                "#24:26",
                "#24:27",
                "#24:28",
                "#24:29",
                "#24:30",
                "#24:31"
            ],
            "Activity_Properties": {
                "comment_count": 0,
                "share_count": 0,
                "like_count": 0
            }
        }

我想要的结果如下

示例2

{
    "@type": "d",
    "@rid": "#28:38",
    "@version": 10,
    "@class": "Activities",
    "activity_type_id": 1,
    "username": "ultimaterandomuser",
    "title": "random new activitie test",
    "image": "http://www.someurl.com/images/blah.png",
    "comments": [
           {
          "username": "randomuser1",
          "comment": "randomuser1 comment"
          "created_at": "2016-06-23 00:00:00",
       },
           {
          "username": "randomuser2",
          "comment": "randomuser2 comment"
          "created_at": "2016-06-23 00:00:00",
       },
           {
          "username": "randomuser3",
          "comment": "randomuser3 comment"
          "created_at": "2016-06-23 00:00:00",
       }
 ],
    "Activity_Properties": {
        "comment_count": 0,
        "share_count": 0,
        "like_count": 0,
    }
}

1 个答案:

答案 0 :(得分:0)

我尝试使用这个简单的数据集:

enter image description here

输出查询1:

SELECT FROM Activities

{
    "result": [
        {
            "@type": "d",
            "@rid": "#12:0",
            "@version": 1,
            "@class": "Activities",
            "activity_type_id": 1,
            "username": "ultimaterandomuser",
            "title": "random new activitie test",
            "image": "http://www.someurl.com/images/blah.png",
            "comments": [
                "#13:0",
                "#13:1",
                "#13:2"
            ],
            "Activity_Properties": {
                "comment_count": 0,
                "share_count": 0,
                "like_count": 0
            },
            "@fieldTypes": "comments=n"
        }
    ],
    "notification": "Query executed in 0.01 sec. Returned 1 record(s)"
}

输出查询2:

SELECT expand(@this.exclude('comments')) FROM (SELECT *, $comments as commentsList FROM Activities
    LET $comments = (SELECT expand(@this.exclude('@rid')) FROM (SELECT username, comment, created_at FROM Comments WHERE @rid IN $parent.current.comments)))

{
    "result": [
        {
            "@type": "d",
            "@rid": "#12:0",
            "@version": 1,
            "@class": "Activities",
            "activity_type_id": 1,
            "username": "ultimaterandomuser",
            "title": "random new activitie test",
            "image": "http://www.someurl.com/images/blah.png",
            "Activity_Properties": {
                "comment_count": 0,
                "share_count": 0,
                "like_count": 0
            },
            "commentsList": [
                {
                    "@type": "d",
                    "@version": 0,
                    "username": "randomuser1",
                    "comment": "randomuser1 comment",
                    "created_at": "2016-06-25 03:33:13",
                    "@fieldTypes": "created_at=t"
                },
                {
                    "@type": "d",
                    "@version": 0,
                    "username": "randomuser2",
                    "comment": "randomuser2 comment",
                    "created_at": "2016-06-25 03:33:13",
                    "@fieldTypes": "created_at=t"
                },
                {
                    "@type": "d",
                    "@version": 0,
                    "username": "randomuser3",
                    "comment": "randomuser3 comment",
                    "created_at": "2016-06-25 03:33:13",
                    "@fieldTypes": "created_at=t"
                }
            ],
            "@fieldTypes": "commentsList=z"
        }
    ],
    "notification": "Query executed in 0.041 sec. Returned 1 record(s)"
}

希望有所帮助