简体   繁体   English

Arangodb AQL 嵌套子查询依赖于另一个数据

[英]Arangodb AQL nested subqueries relying on the data from another

I currently have three collections that need to be routed into one endpoint.我目前有三个集合需要路由到一个端点。 I want to get the Course collection sort it, then from that course, I have to use nested subqueries to fetch a random review(there could be multiple tied to the same course) and also get the related user.我想让 Course 集合对其进行排序,然后从该课程中,我必须使用嵌套子查询来获取随机评论(可能有多个与同一课程相关联)并获取相关用户。

User{
    name:
    _id:User/4638
    key: ...}
Review{
    _from: User/4638
    _to: Course/489
    date: ....}
Course{
   _id: Course/489
   title: ...}

The issue I'm having is fetching the user based on the review.我遇到的问题是根据评论获取用户。 I've tried MERGE, but that seems to limit the query to one use when there should be multiple.我已经尝试过 MERGE,但是当应该有多个时,这似乎将查询限制为一种用途。 Below is the current output using LET.下面是使用 LET 的电流输出。

 "course": {
        "_key": "789",
        "_id": "Courses/789",
        "_rev": "_ebjuy62---",
        "courseTitle": "Pandas Essential Training",
        "mostRecentCost": 15.99,
        "hours": 20,
        "averageRating": 5
      },
      "review": [
        {
          "_key": "543729",
          "_id": "Reviews/543729",
          "_from": "Users/PGBJ38",
          "_to": "Courses/789",
          "_rev": "_ebOrt9u---",
          "rating": 2
        }
      ],
      "user": []
    },

Here is the current LET subquery method I'm using.这是我正在使用的当前 LET 子查询方法。 I was wondering if there was anyway to pass or maybe nest the subqueries so that user can read review.我想知道是否有任何方法可以通过或者嵌套子查询,以便用户可以阅读评论。 Currently I try to pass the LET var but that isn't read in the output since a blank array is shown.目前我尝试传递 LET var,但由于显示了一个空白数组,因此没有在输出中读取它。

    FOR c IN Courses
        SORT c.averageRating DESC
        LIMIT 3 
            LET rev = (FOR r IN Reviews
                FILTER c._id == r._to
                SORT RAND()
                LIMIT 1
                RETURN r)
            LET use = (FOR u IN Users
                FILTER rev._from == u._id
                    RETURN u)
        
            
        RETURN {course: c, review: rev, user: use}`

The result of the first LET query, rev , is an array with one element.第一个LET查询的结果rev是一个包含一个元素的数组。 You can re-write the complete query two ways:您可以通过两种方式重新编写完整的查询:

  1. Set rev to the first element of the LET query result:rev设置为 LET 查询结果的第一个元素:
FOR c IN Courses
        SORT c.averageRating DESC
        LIMIT 3 
            LET rev = (FOR r IN Reviews
                FILTER c._id == r._to
                SORT RAND()
                LIMIT 1
                RETURN r)[0]
            LET use = (FOR u IN Users
                FILTER rev._from == u._id
                    RETURN u)
        
            
        RETURN {course: c, review: rev, user: use}

I use this variant in my own projects.我在自己的项目中使用这个变体。

  1. Access the first elememt og rev in the second LET query:在第二个LET查询中访问第一个 elememt og rev
FOR c IN Courses
        SORT c.averageRating DESC
        LIMIT 3 
            LET rev = (FOR r IN Reviews
                FILTER c._id == r._to
                SORT RAND()
                LIMIT 1
                RETURN r)
            LET use = (FOR u IN Users
                FILTER rev[0]._from == u._id
                    RETURN u)
        
            
        RETURN {course: c, review: rev, user: use}

This is untested, the syntax might need slight changes.这是未经测试的,语法可能需要稍作更改。 And you have to look at cases where there aren't any reviews - I can't say how this behaves in that case from the top of my head.而且您必须查看没有任何评论的情况-我无法从我的脑海中说出这种情况下的行为方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM