Partial Date match based on today's date and month, disregard year

I’m storing user birthdays using the Date function. I need to be able to retrieve users who were born on a specific date (day and month only, i.e. disregarding the year).

Example of a user:

{
  "ref": Ref(Collection("Users"), "370155148899516621"),
  "ts": 1690378914440000,
  "data": {
    "email": "email@example.com",
    "date": Date("2001-07-26")
  }
}

The above user should match an index search for Date("2023-07-26").

I know I can probably use the Equals function to compare the desired date and user dates based on the %tm-%td format, however I’m struggling to compose the query.

Can anyone point me in the right direction? Thank you!

Hello @marianne

You can try to use the format function to get the month and day from the date and then compare them.

Let me know about the result :+1:

1 Like

In FQL v10, Date and Time have additional methods to retrieve specific time components.

An index would make this a very efficient search. And you can index on Computed Fields as well! Here’s an example schema.

// Fauna Schema Language (FSL)
collection Users {
  compute birthday_month = (doc => doc.date.month)
  compute birthday_day = (doc => doc.date.dayOfMonth)

  index by_birthday {
    terms [.birthday_month, .birthday_day]
    // OPTIONAL: add some values you might want to cover in
    // your index, including the whole date field
    values [.date, .name, .email] 
  }
}

And query like this

// FQL
Users.by_birthday(6, 18)

COMING SOON

We are working on implementing indexing directly on properties of types like Date and Time. Once that is available, we’ll be able to define the index without computed fields. But it will perform the same. Under the hood the indexer will work like using a computed field that’s made for you.

  index by_birthday {
    terms [.date.month, .date.dayOfMonth]
  }

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