Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter entities on a sub-entity property in Google Datastore query

My Datastore entities are going to have properties of embedded entity type.

After I save them as following (I'm using gcloud v0.27.0):

dataset.save([{
    key: dataset.key({ path: ['MyKind', 1] }),
        data: {
            foo: 'bar',
            zxc: {
                nested: {
                    foobar: 32
                }
            }
        }
    }, 
    {
        key: dataset.key({ path: ['MyKind', 2] }),
        data: {
            foo: 'a string',
            zxc: {
                nested: {
                    foobar: 132
                }
            }
        }
    }
    ], function(error) { console.log(error); });

Is there any way to query entities that, say, have zxc.nested.foobar=132?

I run query like on the picture below and it shows no result.

enter image description here

like image 666
Evgeny Timoshenko Avatar asked May 18 '16 05:05

Evgeny Timoshenko


1 Answers

You can do this by joining the property names with dots and using that joined string as the property name in the query.

In the Cloud Datastore v1beta3 API, the JSON request would look like:

{
  "query": 
  {
    "kinds": 
    [
      {
        "name": "MyKind"
      }
    ],
    "filter": 
    {
      "propertyFilter": 
      {
        "property": 
        {
          "name": "zxc.nested.foobar"
        },
        "operator": "EQUAL",
        "value": 
        {
          "integerValue": "132"
        }
      }
    }
  }
}

Note in order for a result to appear, each of the properties must be indexed. This is the case by default in the JSON API:

{
  "key": 
  {
    "path": 
    [
      {
        "kind": "MyKind",
        "id": 1
      }
    ]
  },
  "properties": 
  {
    "zxy": 
    {
      "entityValue": 
      {
        "properties": 
        {
          "nested": 
          {
            "entityValue": 
            {
              "properties": 
              {
                "foobar": 
                {
                  "integerValue": "132"
                }
              }
            }
          }
        }
      }
    }
  }
}

Datastore client libraries typically also index properties by default, but some older versions of gcloud-node (e.g. 0.27.0) may not.

like image 50
Ed Davisson Avatar answered Oct 05 '22 15:10

Ed Davisson