Filter by relationship between two predicates

Hello!

I feel like I’m missing something basic on this, and I’ve been going through all the filter documentation out there but to no avail. Imagine I have a collection of nodes, which each have two datetimes, say ‘expectedTime’ and ‘actualTime’.

Now if I want to find out all the situations where the person was ‘late’, which is to say actualTime is after expectedTime, how would I go about filtering the search? First I thought I could simply do this:

lateNodes(func: gt(actualTime, expectedTime)) {
   uid
}

But of course that doesn’t work, with an error saying that it can’t parse “expectedTime” as a date. Maybe something to do with variables? But my various attempts never worked. If I store a variable for each time, that represents all the times for the nodes, not for a single node.

So, given a collection of nodes with those two values, how do I select only those where one is after the other?

The second argument in comparisons should be a value. You’ll want to pull all of the expected times into a variable before passing into the query.

var(func: has(expectedTime)) {
expected as expectedTime
}

lateNodes(func: gt(actualTime, expected)) {
   uid
}

Thanks for getting back to me!

I thought of doing that, but it doesn’t work. For example, consider this small data set where two people (Michael and Raquel) are late and one person (Paul is on time). I set it like so:

{
  "set": [
    {
      "name": "Michael (late)",
      "expectedTime": "2021-03-04T18:00:00Z",
      "actualTime": "2021-03-04T18:05:00Z"
    },
    {
      "name": "Raquel (late)",
      "expectedTime": "2021-03-04T22:00:00Z",
      "actualTime": "2021-03-04T22:01:00Z"
    },
    {
      "name": "Paul (on time)",
      "expectedTime": "2021-03-04T20:00:00Z",
      "actualTime": "2021-03-04T19:59:00Z"
    }]
}

Now I run the search as you suggest:

{
  var(func: has(expectedTime)) {
    expected as expectedTime
  }
  
  lateNodes(func: gt(actualTime, val(expected))) {
    uid
  }
}

And I get the error:

{
  "name": "t",
  "url": "http://localhost:8082/query?debug=true",
  "errors": [
    {
      "code": "ErrorInvalidRequest",
      "message": ": gt expects only 1 argument. Got: [2021-03-04T20:00:00Z 2021-03-04T18:00:00Z 2021-03-04T22:00:00Z]"
    }
  ]
}

Which I suppose makes sense… the variabled “expected” contains a bunch of values. How do I tell dgraph to focus on specifically the value for the node in question?

Any help would be appreciated

Michael