First value from index

Hi,
I have a Collection: maps with documents like this:

{
  "ref": Ref(Collection("maps"), "328843776032243783"),
  "ts": 1649868713320000,
  "data": { "mapId": 12322 }
}

and a unique index:

{
  name: "all_maps_mapId",
  unique: true,
  serialized: true,
  source: "maps",
  values: [
    {
      field: ["data", "mapId"],
      reverse: true
    }
  ]
}

I want a UDF to extract the first value from this index (i.e. the maximum mapId) in the most efficient way. Here’s what I have so far for my function body:

Query(
  Lambda(
    [],
    Select(
      [0],
      Select("data", Paginate(Match(Index("all_maps_mapId")), { size: 1 }))
    )
  )
)

Is there a better (faster, more elegant, etc…) way to accomplish this? I tried using the built-in function Take, but it needs an array…
Thanks!

If you use Get, it will get you the first value as a doc from a match result.
See here

Query(
  Lambda(
    [],
    Get(Match(Index("all_maps_mapId")))
  )
)
2 Likes

Thanks @wallslide – I need to read more about Get! I also realized I can use a path in Select, so my UDF body works now with

Query(
  Lambda(
    [],
    Select(['data','mapId'], 
      Get(Match(Index("all_maps_mapId")))
    )
  )
)
2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.