Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse data from Fauna DB

I have been looking at the docs indexes for FQL and Fauna DB. Is there a way to reverse the order of the data returned from the query?

Here is the code I have:

q.Map(
  q.Paginate(q.Match(q.Index("all_pages")), {
    //! Find way to change order
    size: 3
  }),
  ref => q.Get(ref)
)

The docs mention the reverse flag.

Does anyone know how to use it?

like image 251
Tithos Avatar asked Oct 29 '25 08:10

Tithos


1 Answers

Imagine you have a collection that contains documents with a price field, let's call it ... ermm ... priceables!

Let's add two documents to the collection with price 1 and price 2.

[{
  "price": 1
},
{
  "price": 2
}]

Image in UI console of the documents

Create a regular index

Create a regular range index on that price value with the following snippet:

CreateIndex({
  name: "regular-value-index",
  unique: false,
  serialized: true,
  source: Collection("priceables"),
  terms: [],
  values: [
    {
      field: ["data", "price"]
    }
  ]
})

Create a reverse index

There can be multiple values (e.g. a composite index) and the reverse field can be set per value. To create a reverse index, set reverse to true for that specific value.

CreateIndex({
  name: "reverse-value-index",
  unique: false,
  serialized: true,
  source: Collection("priceables"),
  terms: [],
  values: [
    {
      field: ["data", "price"],
      reverse: true
    }
  ]
})

If you go to the UI console and open the index you will notice the values are sorted from highest to low. UI console, reverse index

I assume that what confused you is that you can't set the reverse boolean in the UI yet. But you can just go to Shell and paste in the FQL code to create the index: Image of the shell

like image 109
Brecht De Rooms Avatar answered Oct 30 '25 23:10

Brecht De Rooms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!