【问题标题】:Is it possible to query Entities in Google Cloud Datastore that have no descendants?是否可以查询 Google Cloud Datastore 中没有后代的实体?
【发布时间】:2018-08-07 00:54:47
【问题描述】:

有没有办法返回在 Google Cloud Datastore 中没有后代的实体?

【问题讨论】:

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


    【解决方案1】:

    如果您的问题是您是否可以检索没有后代的实体,那么可以。您可以通过其键(或通过查询)检索任何实体。

    但是,如果您打算运行检索所有无子实体的查询,则这是不可能的。 ancestry 信息存储在后代实体中,因此您应该恢复所有实体的所有祖先键(通过projection query),存储其祖先的所有键,然后运行查询对于所有实体检查那些不是任何实体的祖先的实体。

    在 shell 中使用 curl 和 jq,可能是这样的:

    export ancestors=$(gcurl -s -H'content-type:application/json' "https://datastore.googleapis.com/v1/projects/$(gcloud config get-value project):runQuery?fields=batch%2FentityResults%2Fentity%2Fkey%2Fpath" -d"{
     \"partitionId\": {
      \"projectId\": \"$(gcloud config get-value project)\",
      \"namespaceId\": \"namespace_id\"
     },
     \"query\": {
      \"kind\": [
       {
        \"name\": \"descendant_entity_name\"
       }
      ],
      \"projection\": [
       {
        \"property\": {
         \"name\": \"__key__\"
        }
       }
      ]
     }
    }" | jq '[.batch.entityResults[].entity.key.path | select(length > 1 ) | .[-2].id]')
    
    gcurl -H'content-type:application/json' "https://datastore.googleapis.com/v1/projects/$(gcloud config get-value project):runQuery?fields=batch%2FentityResults%2Fentity%2Fkey%2Fpath" -d"{
     \"partitionId\": {
      \"projectId\": \"$(gcloud config get-value project)\",
      \"namespaceId\": \"namespace_id\"
     },
     \"query\": {
      \"kind\": [
       {
        \"name\": \"ancestor_entity_name\"
       }
      ],
      \"projection\": [
       {
        \"property\": {
         \"name\": \"__key__\"
        }
       }
      ]
     }
    }" | jq '.batch.entityResults[].entity.key.path[-1].id | select(inside(env.ancestors)|not)'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 2021-04-05
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多