简体   繁体   English

是否可以查询Google Cloud Datastore中没有后代的实体?

[英]Is it possible to query Entities in Google Cloud Datastore that have no descendants?

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

If your question is if you can retrieve an entity that has no descendants, then yes. 如果您的问题是是否可以检索没有后代的实体,则可以。 You can retrieve any entity through their key (or through a query). 您可以通过其键(或通过查询)检索任何实体。

However, if you intend to run a query that retrieves all the child-less entities, this will not be possible. 但是,如果您打算运行一个检索所有无子实体的查询,则将不可能。 The ancestry information is stored in the descendant entities, so you should recover all the ancestor keys for all the entities (through a projection query ), store all the keys for their ancestors, and then run a query for all the entities checking those that are not ancestors to any entity. 祖先信息存储在后代实体中,因此您应该(通过投影查询 )恢复所有实体的所有祖先键,存储其祖先的所有键,然后对所有实体运行查询以检查那些不是任何实体的祖先。

Using curl and jq in a shell, it could be something like the following: 在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)'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM