我将如何编写查询以提供以下结果?

How would I write a query to give me the following results?

我有 2 个 类。

活动和评论。

活动,链接到评论。

评论,包含字段用户名、评论和 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:

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)"
}

希望对您有所帮助