是否可以返回Google Cloud Datastore中没有后代的实体?
答案 0 :(得分:0)
如果您的问题是是否可以检索没有后代的实体,则可以。您可以通过其键(或查询)检索任何实体。
但是,如果您打算运行一个查询以检索所有少子实体,则将不可能。 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)'