Take the 2-minute tour ×

Is it possible to find out the index of an entity? For example, my users are creating thousands of Foos. I can locate an arbitrary Foo instance using its key, but I don't know its index compared to all other Foos. I'd like to figure out that "this is Foo 12 out of 20,000". I only have one sort order for Foos. Is something like that possible?


share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Not trivially.

If you know the key of your entity, you could do a keys-only query using your sort order and just iterate over (and count) the returned keys until hit the one you're looking for. You can use query cursors and task chaining to split the job into reasonable chunks.

If your index is just the order of creation, you could alternately de-normalize by storing a single counter entity. Each time you want to create a new Foo, transactionally lock the counter object, increment it, and then write back the new counter value to the counter and your new Foo. This does prevent you from creating multiple Foo entities at once, although if you don't need an ID returned to the user immediately, you could defer entity creation to a task.

share|improve this answer
add comment

Your Answer


