I have a very simple collection with documents that look like this:
{
...
latestEdit: Time(...),
lastPublished: Time(...)
}
I would like to query all documents that have a latestEdit time that's after lastPublished time.
I find FQL to be very different to SQL and I'm finding the transition quite hard.
Any help much appreciated.
Fauna's FQL is not declarative, so you have to construct the appropriate indexes and queries to help you solve problems like this.
Fauna indexes have a feature called "bindings", which allow you to provide a user-defined function that can compute a value based on document values. The binding lets us index the computed value by itself (rather than having to index on
latestEditorlastPublished). Here's what that might look like:You can see that we define a binding called
needsPublish. The binding usesLetto define named values for the two document fields that we want to compare, and then theIfstatement checks to see if thelatestEditvalue is greather thanlastPublishedvalue: when it is we returntrue, otherwise we returnfalse. Then, the binding is used in the index'stermsdefinition, which defines the fields that we want to be able to search on.I created sample documents in a collection called
test, like so:The first document subtracts one day from
lastPublishedand the second document subtracts one day fromlatestEdit, to test both conditions of the binding.Then we can query for all documents where
needsPublishresults intrue:And we can also query for all documents where
needsPublishisfalse: