Mount Query to last Google Datastore record

Asked

Viewed 98 times

3

I’m working with the Datastore from Google and I’m unable to mount a Query to return what I need.

Situation

I have a Entity, keyed {"item": codItem, "item": id}, still within that Entity I have the property codCliente.

For example we could have a JSON thus being saved (id is the field of registration google generates):

[
  {"id":2137847312334,"codItem":12,"codCliente":1,"dataRegistro":2017-04-23T18:25:43.511Z},
  {"id":2183462352427,"codItem":15,"codCliente":1,"dataRegistro":2017-04-23T18:25:43.511Z},          
  {"id":9128734678236,"codItem":10,"codCliente":1,"dataRegistro":2017-04-23T18:25:43.511Z},

  {"id":2137847312334,"codItem":12,"codCliente":1,"dataRegistro":2017-03-20T18:25:43.511Z},         
  {"id":2183462352427,"codItem":15,"codCliente":1,"dataRegistro":2017-03-20T18:25:43.511Z},         
  {"id":9128734678236,"codItem":10,"codCliente":1,"dataRegistro":2017-03-20T18:25:43.511Z}
]

Problem

When I mount the query to search for these results filtering by client, with the codCliente I still don’t have the information of which codItem that he has.

And what I need is a way to only seek one (only one) result from each codItem of codCliente referenced with the dataRegistro newer. This way with the above example I should have to return the first three JSON records.

I’m open to changes in structure and json document keys. The only thing there is no changing is about having the information of which codItem the customer has before making the query. Any idea?

1 answer

0


I decided as follows:

  • A query that returns only the codes of the items filtered by codCliente, using the functions of google cloud Projection and DistinctOn
  • A second query that returns only one record, using FetchOptions.Builder.withLimit(1), filtered by codItem and codCliente ordered by date

Query to search only the password:

public QueryResults<ProjectionEntity> getItem(int codCliente) {

   Query<ProjectionEntity> query2 = Query
    .newProjectionEntityQueryBuilder()
    .setKind("Item")
    .setProjection("CodItem")
    .setDistinctOn("CodItem")
    .setFilter(PropertyFilter.eq("codCliente", codCliente))
    .build();

  QueryResults<ProjectionEntity> tasks = getDatastore().run(query2);
  return tasks;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.