获取 ORDERBY Faunadb

Get ORDERBY Faunadb

data: [
{
  name: "Ivo Pereira",
  image: "https://avatars.githubusercontent.com/u/55932505?v=4",
  email: "ivojosepereira.jr@gmail.com",
  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: "lauramariapereira.23@gmail.com",
  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: "lauramariapereira.23@gmail.com.br",
  level: "55",
  experience: "0",
  challenges_completed: "0",
  current_experience_to_next_level: "0"
}

] }

大家好,我做了一些研究,但仍然没有找到任何可以帮助我的东西。 我想把“级别”字段降序排列,但我只能从 faunadb

得到这个反馈

对于 Fauna,您需要使用索引来控制结果是升序还是降序。

您没有提供用于提供上述文档列表的查询,因此我假设它们在 Users 集合中。

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

使用该索引,您可以报告按 level 降序排列的所有用户,并使用此查询:

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: 'lauramariapereira.23@gmail.com.br',
        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: 'lauramariapereira.23@gmail.com',
        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: 'ivojosepereira.jr@gmail.com',
        level: '100',
        experience: '70',
        challenges_completed: '1',
        current_experience_to_next_level: '70'
      }
    }
  ]
}

如您所见,顺序“更好”,但不是很好。由于数据中的 level 字段是字符串,因此此结果涉及字符串排序(任何以 1 开头的字符串都小于任何以 5 开头的字符串)。

相反,如果您的数据使用数值作为级别,结果应该是您想要的。

这是一个批量更新数据的查询,最多 64 个文档的默认分页限制:

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

更新level字段后,查询得到排序后的结果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: 'ivojosepereira.jr@gmail.com',
        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: 'lauramariapereira.23@gmail.com.br',
        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: 'lauramariapereira.23@gmail.com',
        level: 5,
        experience: '0',
        challenges_completed: '0',
        current_experience_to_next_level: '0'
      }
    }
  ]
}