How to get a shorted list of documents?

Hello,
My documents all have a user_id and I would like to go through them and return a limited amount (as example 10 or 50) to the user. I think that that would be better in case the user has a lot of documents (if I am wrong it would probably make it easier since I could handle the limits, sorting and more on the backend code instead of directly in the database).
I have tried something like this:
Foreach(Paginate(Match(Index(“user”), “user id here”)), Lambda(“link”, Get(Select([“Ref”], Var(“link”)))))
Which sadly didnt work. I hope that there is a better way to solve this problem. this is the first time I am using lambda so there is a good chance I am doing something wrong
I would like to have the data (not just the refs) in the end if possible.
Thanks in advance!

To return the full documents you need to use Map (Map | Fauna Documentation) instead of Foreach in this query.
To return a limited set, there is a parameter that you can pass to paginate called ‘size’ (Paginate | Fauna Documentation)

Hello,
Thanks for your quick response. I am not sure what I should “Select” (I think I need to select to get it to get the real full document?)
Currently I am just getting this after the paginate:
image
Using “ref” sadly doesnt work

What does your “user” index look like" The output of Get(Index("user")) would be helpful.

When you call Paginate on the results of Match, the page of results contains the values from the index’s values field. If there are no values specified, the page contains references.

In your Lambda, only “link” is accepted as a parameter. What is “link”? If it is a reference, then the Select call is unnecessary: Get can fetch a document by its reference.

To return a limited amount of information from a document, but more than one field, Let is your friend. For example, suppose we had this document:

Get(Ref(Collection("stores"), "301"))
{
  ref: Ref(Collection("stores"), "301"),
  ts: 1622738396890000,
  data: {
    name: 'DC Fruits',
    address: {
      street: '13 Pierstorff Drive',
      city: 'Washington',
      state: 'DC',
      zipCode: '20220'
    }
  }
}

If you only want the name and ZIP code in the results, then this would work:

Let(
  {
    doc: Get(Ref(Collection("stores"), "301"))
  },
  {
    name: Select(["data", "name"], Var("doc")),
    zip: Select(["data", "address", "zipCode"], Var("doc")),
  }
)
{ name: 'DC Fruits', zip: '20220' }

In the first parameter to Let, we fetch the document and store that result in a value named doc.

The second parameter to Let is how we compose the return value, which is basic object containing the name and zip fields. Because the document is captured in doc, we can use the Select statements to gather the fields that we need.

To apply that to all of the paginated documents, the query would look more like this:

Map(
  Paginate(Documents(Collection("stores"))),
  Lambda(
    "ref",
    Let(
      {
        doc: Get(Var("ref")),
      },
      {
        name: Select(["data", "name"], Var("doc")),
        zip: Select(["data", "address", "zipCode"], Var("doc")),
      }
    )
  )
)
{
  data: [
    { name: 'DC Fruits', zip: '20220' },
    { name: 'Party Supplies', zip: '20002' },
    { name: 'Foggy Bottom Market', zip: '20037' }
  ]
}

Note: these sample documents are part of the sample data available when you create a new database in the Dashboard and check the “Pre-populate with demo data” checkbox.

I used the Documents function, which uses an internal index covering all of the documents in the specified collection, and it only returns each existing document’s reference.

For your query, the query would need to be adapted to suit your index definition. If the specific fields you want are always needed for your workflow, you could include those fields in the index’s values definition and then you could avoid calling Get and Select altogether.

Hello,
This is my user index:
image
I will try your solution and update this topic if it fixed my issue. Thanks a lot for the help!

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