How to fetch newest entities from Google Datastore?

I am trying to query entities from Google Datastore and would like to fetch the 10 newest entities based on their time created property, but I am unsure about the functionality of the addSort method.

Will the addSort method fetch the newest 10 entities if I sort on the timeCreated property and limit the results to 10, or will this just fetch the entities using the default order and sort those results?

Here is the Query using the addSort method:

   Query q1 = new Query("Person").addSort("timeCreated", SortDirection.DESCENDING);

Upvotes: 0

Views: 38

Answers (1)

Jim Morrison
Jim Morrison

Reputation: 2887

The sort order affects the index that is scanned for the query, so the query (with a limit) will return the most recently entities.

Upvotes: 1

Related Questions