0
votes

I have a Google Cloud Datastore Kind, which has the property DataTime. This property is from the type DateTime.

I want to create a GQL statement the will return all the rows before two days. For example:

SELECT * FROM `Tracing-V1` WHERE StartTime < DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL -2 DAY); 

I found here the function of CURRENT_TIMESTAMP()andDATE_ADD`. Unfortunately, the GQL don't recognize those functions.

enter image description here

I get back the error message:

GQL Query error: Unknown function "DATE_ADD".

Anyone know why? How should I implement it?

1

1 Answers

1
votes

According to the GQL Reference, the Cloud Datastore GQL only supports the DATETIME and KEY functions, whose functionalities are described in the Synthetic literals section.

Alternatively, you could do it programmatically, with any supported programming language. Here’s an example of how it would look like in python:

from datetime import datetime, timedelta
from google.cloud import datastore

client = datastore.Client()
now = datetime.now()
now = now.replace(hour=0, minute=0, second=0, microsecond=0)

the_day_before = now - timedelta(days=2)
query = client.query(kind='Test')

query.add_filter('date', '>', the_day_before)
result = list(query.fetch())
print(result)