Search code examples
faunadb

Get ORDERBY Faunadb


data: [
{
  name: "Ivo Pereira",
  image: "https://avatars.githubusercontent.com/u/55932505?v=4",
  email: "[email protected]",
  level: "100",
  experience: "70",
  challenges_completed: "1",
  current_experience_to_next_level: "70"
},
{
  name: "Laura pereira",
  image: "https://avatars.githubusercontent.com/u/86892146?v=4",
  email: "[email protected]",
  level: "5",
  experience: "0",
  challenges_completed: "0",
  current_experience_to_next_level: "0"
},
{
  name: "Laura pereira maria",
  image: "https://avatars.githubusercontent.com/u/86892146?v=4",
  email: "[email protected]",
  level: "55",
  experience: "0",
  challenges_completed: "0",
  current_experience_to_next_level: "0"
}

] }

Hi everyone, I did some research but I still haven't found anything to help me. I want to put the "level" field in descending order, but I can only get this feedback from faunadb


Solution

  • With Fauna, you need to use an index to control whether results are in ascending or descending order.

    You haven't provided the query that you used that provided the list of documents above, so I'll assume that they are in the Users collection.

    CreateIndex({
      name: "Users_by_level_descending",
      source: Collection("Users"),
      values: [
        { field: ["data", "level"], reverse: true },
        { field: ["ref"] },
      ]
    })
    

    With that index, you can report all users sorted by level descending with this query:

    Map(
      Paginate(
        Match(Index("Users_by_level_descending"))
      ),
      Lambda(
        ["level", "ref"],
        Get(Var("ref"))
      )
    )
    {
      data: [
        {
          ref: Ref(Collection("Users"), "304066349713326592"),
          ts: 1626239118210000,
          data: {
            name: 'Laura pereira maria',
            image: 'https://avatars.githubusercontent.com/u/86892146?v=4',
            email: '[email protected]',
            level: '55',
            experience: '0',
            challenges_completed: '0',
            current_experience_to_next_level: '0'
          }
        },
        {
          ref: Ref(Collection("Users"), "304066330657554944"),
          ts: 1626239100040000,
          data: {
            name: 'Laura pereira',
            image: 'https://avatars.githubusercontent.com/u/86892146?v=4',
            email: '[email protected]',
            level: '5',
            experience: '0',
            challenges_completed: '0',
            current_experience_to_next_level: '0'
          }
        },
        {
          ref: Ref(Collection("Users"), "304066303135580672"),
          ts: 1626239073800000,
          data: {
            name: 'Ivo Pereira',
            image: 'https://avatars.githubusercontent.com/u/55932505?v=4',
            email: '[email protected]',
            level: '100',
            experience: '70',
            challenges_completed: '1',
            current_experience_to_next_level: '70'
          }
        }
      ]
    }
    

    As you can see, the order is "better", but not great. Since the level field in your data is a string, this result involves string sorting (any string starting with a 1 is less than any string starting with a 5).

    Instead, if your data uses a numeric value for the level, the result should be what you want.

    Here's a query to update your data in bulk, up to the default pagination limit of 64 documents:

    Map(
      Paginate(Documents(Collection("Users"))),
      Lambda(
        "ref",
        Let(
          { doc: Get(Var("ref")) },
          Update(
            Var("ref"),
            {
              data: {
                level: ToInteger(
                  Select(["data", "level"], Var("doc"))
                )
              }
            }
          )
        )
      )
    )
    

    After the update to the level field, the query to get the sorted results returns:

    Map(
      Paginate(
        Match(Index("Users_by_level_descending"))
      ),
      Lambda(
        ["level", "ref"],
        Get(Var("ref"))
      )
    )
    {
      data: [
        {
          ref: Ref(Collection("Users"), "304066303135580672"),
          ts: 1626239549120000,
          data: {
            name: 'Ivo Pereira',
            image: 'https://avatars.githubusercontent.com/u/55932505?v=4',
            email: '[email protected]',
            level: 100,
            experience: '70',
            challenges_completed: '1',
            current_experience_to_next_level: '70'
          }
        },
        {
          ref: Ref(Collection("Users"), "304066349713326592"),
          ts: 1626239549120000,
          data: {
            name: 'Laura pereira maria',
            image: 'https://avatars.githubusercontent.com/u/86892146?v=4',
            email: '[email protected]',
            level: 55,
            experience: '0',
            challenges_completed: '0',
            current_experience_to_next_level: '0'
          }
        },
        {
          ref: Ref(Collection("Users"), "304066330657554944"),
          ts: 1626239549120000,
          data: {
            name: 'Laura pereira',
            image: 'https://avatars.githubusercontent.com/u/86892146?v=4',
            email: '[email protected]',
            level: 5,
            experience: '0',
            challenges_completed: '0',
            current_experience_to_next_level: '0'
          }
        }
      ]
    }