Skip to content

Instantly share code, notes, and snippets.

@colllin
Last active April 25, 2020 01:34
Show Gist options
  • Save colllin/230d2f518ba182f527a37b33d2aca2e0 to your computer and use it in GitHub Desktop.
Save colllin/230d2f518ba182f527a37b33d2aca2e0 to your computer and use it in GitHub Desktop.
> CreateIndex({
    name: 'users_by_messageSentAt_desc',
    source: Collection('messages'),
    values: [
        {field: ['data','sentAt'], reverse: true},
        {field: ['data','fromUser']}
    ]
})
> Paginate(Match(Index('users_by_messageSentAt_desc')), {size: 5})
{
  after: [
    Time("2020-03-21T23:57:10.250055Z"),
    Ref(Collection("users"), "260651016991539721"),
    Ref(Collection("messages"), "260651017345958409")
  ],
  data: [
    [
      Time("2020-03-22T00:08:15.085656Z"),
      Ref(Collection("users"), "260651714031387145")
    ],
    [
      Time("2020-03-22T00:06:20.025334Z"),
      Ref(Collection("users"), "260651531102061065")
    ],
    [
      Time("2020-03-22T00:05:20.643705Z"),
      Ref(Collection("users"), "260651531102061065")
    ],
    [
      Time("2020-03-22T00:04:03.398751Z"),
      Ref(Collection("users"), "260651450109002241")
    ],
    [
      Time("2020-03-22T00:02:15.383899Z"),
      Ref(Collection("users"), "260651016991539721")
    ]
  ]
}
> Paginate(
    Join(
        Match(Index('users_by_messageSentAt_desc')),
        Lambda(['sentAt', 'userRef'], Singleton(Var('userRef')))
    ),
    {size: 50}
)
{
  data: [
    Ref(Collection("users"), "253281582578663937"),
    Ref(Collection("users"), "254468813583548938"),
    Ref(Collection("users"), "256363858946425362"),
    Ref(Collection("users"), "256410871370088978"),
    Ref(Collection("users"), "256915985423925780"),
    Ref(Collection("users"), "259736231924990482"),
    Ref(Collection("users"), "260192161960559122"),
    Ref(Collection("users"), "260644259865035273"),
    Ref(Collection("users"), "260649253533123081"),
    Ref(Collection("users"), "260650998105637386"),
    Ref(Collection("users"), "260651016991539721"),
    Ref(Collection("users"), "260651450109002241"),
    Ref(Collection("users"), "260651531102061065"),
    Ref(Collection("users"), "260651714031387145")
  ]
}

Note that the first 5 index items I returned contained contacts in the order ...145, ...065, ...241, ...721, and in this last full list, we have a distinct list of contacts but the order has been reversed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment