Search code examples
arangodbaql

Unable to extract object value from dataset to compare or to apply filter on


I am trying to solve Q.22 on this page. The dataset is here

Q.22 Write a MongoDB query to find the restaurant Id, name, and grades for those restaurants which achieved a grade of "A" and scored 11 on an ISODate "2014-08-11T00:00:00Z" among many of survey dates

Data is in the form as:

[
  {
    "_key": "2451925",
    "_id": "restaurants/2451925",
    "_rev": "_buC4Kd2---",
    "address": {
      "building": "1007",
      "coord": [
        -73.856077,
        40.848447
      ],
      "street": "Morris Park Ave",
      "zipcode": "10462"
    },
    "borough": "Bronx",
    "cuisine": "Bakery",
    "grades": [
      {
        "date": {
          "$date": 1393804800000
        },
        "grade": "A",
        "score": 2
      },
      {
        "date": {
          "$date": 1378857600000
        },
        "grade": "A",
        "score": 6
      },
      {
        "date": {
          "$date": 1358985600000
        },
        "grade": "A",
        "score": 10
      },
      {
        "date": {
          "$date": 1322006400000
        },
        "grade": "A",
        "score": 9
      },
      {
        "date": {
          "$date": 1299715200000
        },
        "grade": "B",
        "score": 14
      }
    ],
    "name": "Morris Park Bake Shop",
    "restaurant_id": "30075445"
  },
  {
    "_key": "2451926",
    "_id": "restaurants/2451926",
    "_rev": "_buC4Kd2--A",
    "address": {
      "building": "469",
      "coord": [
        -73.961704,
        40.662942
      ],
      "street": "Flatbush Avenue",
      "zipcode": "11225"
    },
    "borough": "Brooklyn",
    "cuisine": "Hamburgers",
    "grades": [
      {
        "date": {
          "$date": 1419897600000
        },
        "grade": "A",
        "score": 8
      },
      {
        "date": {
          "$date": 1404172800000
        },
        "grade": "B",
        "score": 23
      },
      {
        "date": {
          "$date": 1367280000000
        },
        "grade": "A",
        "score": 12
      },
      {
        "date": {
          "$date": 1336435200000
        },
        "grade": "A",
        "score": 12
      }
    ],
    "name": "Wendy'S",
    "restaurant_id": "30112340"
  }]

I am trying to filter out dates with the given ISODate above. I'm using the date_timestamp function to convert the above to the way the dates are stored in the data.

I am not able to filter the dates as they seem to be wrapped in {"$date":1393804800000}. Trying a loop within g says that an object is found instead of a collection or array. Applying a filter and checking equality with the date_timestamp() returns arrays of nulls [],[] etc.

for r in restaurants
    for g in r.grades
       let d=date_timestamp("2014-08-11T00:00:00Z")
       return {date:g.date,target_date:d}

Please Help, Thank you


Solution

  • You have to filter for the field inside the object:

    LET target_date = DATE_TIMESTAMP('2014-08-11T00:00:00Z')
    FOR r IN restaurants
      FOR g IN r.grades
        FILTER g.date.$date == target_date
        RETURN {date: g.date}