Retrieving the first document from a Match result

The Get docs say:

Get is also useful for retrieving the first document from a Match result.

This seems like it should be a simple query. I have a sessions_by_code unique index and I want to get a session by its code.

This is the only query I could get to work, but surely there is a better way without Map/Paginate?

await client.query(
  q.Map(
    q.Paginate(q.Match(q.Index('sessions_by_code'), 'ABCD')), // ABCD is the code
    q.Lambda('ref', q.Var('ref'))
  )
);

Hi shun,

if you are certain that there is only one document (I am actually not 100% sure if it works when there are multiple docs) then you can write:

await client.query(
  q.Get(q.Match(q.Index('sessions_by_code'), 'ABCD'))
);

If there are multiple documents and you just want to get the first:

await client.query(
  q.Select([0], q.Paginate(q.Match(q.Index('sessions_by_code'), 'ABCD')))
);

Hello there… just to be a little more accurate, you CAN perform a GET + MATCH on a Index that is not unique… it will just get the first element:

> Get(Match(Index('foo_idx')))
{
  ref: Ref(Collection("kids_foo"), "273223111269155328"),
  ts: 1596824713910000,
  data: { idx: 4, name: 'foo_4' }
}
> Paginate(Match(Index('foo_idx')))
{
  data: [
    Ref(Collection("kids_foo"), "273223111269155328"),
    Ref(Collection("kids_foo"), "273223111269156352"),
    Ref(Collection("kids_foo"), "273223111269157376"),
    /// bla bla bla
  ]
}
3 Likes