Mount Query for Last Record NoSQL Google Datastore

3

I'm working with Google's Datastore and I'm not getting a Query to get back what I need.

Situation

I have a Entity , with key pair {"item": codItem, "item": id} , still inside this Entity I have the property codCliente.

For example, we could have a JSON like this saved ( id is the registration field that 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 run the query to search these results by filtering by client, with codCliente I still do not have the information of what codItem it has.

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

I am open to json document structure and key changes. The only thing that can not be changed is about having the information of which codItem the client has before making the query. Any ideas?

    
asked by anonymous 09.06.2017 / 15:39

1 answer

0

I have decided as follows:

  • A query that returns only item codes by filtering by codCliente , using google cloud Projection and DistinctOn
  • A second query that returns only one record, using FetchOptions.Builder.withLimit(1) , filtered by codItem and codCliente sorted by date

Query to search only the codItem:

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;

}
    
14.06.2017 / 14:21