Is it possible to query a many to many link and get a shallow set or array?

67 views Asked by At

Lets assume this simplified schema:

type default::Genre {
  required property name -> std::str {
    constraint std::exclusive;
  };
};

type default::Movie {
  required property title -> std::str;
  property year -> std::int64;
  multi link genres -> default::Genre;
};

When I query the movies like this:

select Movie {
  title,
  year,
  genres: {name}
}
limit 1

I get this response:

[
  {
    "title": "Troja",
    "year": 2004,
    "genres": [
      {
        "name": "Action"
      },
      {
        "name": "War"
      },
      {
        "name": "Adventure"
      },
      {
        "name": "History"
      }
    ]
  }
]

Is there a way to change the query to get this response instead

[
  {
    "title": "Troja",
    "year": 2004,
    "genres": [
      "Action", "War", "Adventure", "History"
    ]
  }
]

I looked through the documentation but I also don't what keyword to search for.

1

There are 1 answers

0
HWM-Rocker On BEST ANSWER

Now I figured out how to write the query to do this

SELECT Movie {
  title,
  year,
  genre_list := .genres.name
}
LIMIT 1

This will return a set, but the corresponding JSON will show a list:

[
  {
    "title": "Troja",
    "year": 2004,
    "genre_list": [
      "Action",
      "War",
      "Adventure",
      "History"
    ]
  }
]

If you really need an array instead you can use the array_agg function like this:

SELECT Movie {
  title,
  year,
  genre_list := array_agg(.genres.name)
}
LIMIT 1