【问题标题】:Datastore GQL with Datetime functions具有日期时间函数的数据存储区 GQL
【发布时间】:2020-01-20 05:43:11
【问题描述】:

我有一个 Google Cloud Datastore Kind,它具有 DataTime 属性。此属性来自DateTime 类型。

我想创建一个 GQL 语句,它将返回两天前的所有行。例如:

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

我找到hereCURRENT_TIMESTAMP()andDATE_ADD`的函数。不幸的是,GQL 无法识别这些函数。

我收到错误消息:

GQL Query error: Unknown function "DATE_ADD".

有人知道为什么吗?我应该如何实现它?

【问题讨论】:

    标签: google-cloud-platform google-cloud-datastore gql gqlquery


    【解决方案1】:

    根据GQL Reference,Cloud Datastore GQL 仅支持 DATETIME 和 KEY 函数,其功能在Synthetic literals 部分中描述。

    或者,您可以使用任何supported programming language 以编程方式执行此操作。这是一个在 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)
    

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 2019-07-30
      • 2014-01-16
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多