124697
124697

Reputation: 21891

How can I get the last 10 records added to a datastore?

I am storing entities like such

Entity entity = new Entity("Feedback");
entity.setProperty("text", searchText);
datastore.put(entity).getId();

How can I query the datastore for the last 10 feedback entries stored?

What I have now returns everything. with a limit it returns the first 10. How can I return the last ten?

Query query = new Query("FeedBack");
List<Entity> feeds = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());

Upvotes: 0

Views: 291

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41099

When you add your entities to the Datastore, you have no guarantee that they will be returned in any particular order. If you want entities to be sorted according to some criteria, you must use an indexed property and set a sort order on this property in your query.

In your case, you need to add a property "Date" and add a timestamp to each entity before saving it:

entity.setProperty("date", new Date().getTime());

Now you can add a sort order to your query - ascending or descending - and set a limit to 10.

Upvotes: 2

Related Questions