1

I have let's say 1000 key names whose existance I want to check in the Google App Engine datastore, but without fetching the entities themselves. One of the reason, beside possible speedups, is that keys-only fetching is free (no cost).

ndb.get_multi() allows me to pass in the list of keys, but it will retrieve the entities. I need a function to do just that but without fetching the entities, but just True or False based whether the specified keys exist.

4

1 に答える 1

2

私はおそらくキーのみのクエリを使用します...:

q = EntityKind.query(EntityKind.key.IN(wanted_keys))
keys_present = set(q.iter(keys_only=True))

これにより、データストアに実際に存在するkeys_present一連のキーがwanted_keys得られます。キーからブールへの目的のマッピングとはまったく同じではありませんが、後者は簡単に構築できます。

key_there = {k: (k in keys_present) for k in wanted_keys}

...実際にそれが必要な場合 (値dictを持つaboolは、通常、!- の扱いにくいハックである可能性が高くなりますset)。

于 2015-03-26T03:53:09.173 に答える