对象查询:按日期过滤 [英] Objectify queries: filter by date

查看:144
本文介绍了对象查询:按日期过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GAE,我需要写一个Objectify查询,要求在9月1日之后创建的元素。



dateCreated字段是Java.util.Date和它以这种格式存储2012-11-29 16:03:59.494000。



此请求不工作:

  public List< MyElement> listAllFromUser(String userId)
{
Objectify ofy = ObjectifyService.begin();
查询< MyElement> q = ofy.query(MyElement.class).filter(dateCreated>,2013-09-01 00:00:00);

列表< MyElement> results = q.list();

返回结果;
}


解决方案


  1. 您的列需要索引



    @Indexed
    protected Date dateCreated;



  2. 您可以按请求中的一列进行排序(>和<和!=都被视为排序,因为索引被排序,然后进行分析) p>您可以通过松动精度对日期进行分组:2012-11-29 16:03:59.494000成为2012-11-29
    这样,您可以安全使用==。为每个精度创建一个列(日,周,月)。一般来说,尽量选择尽可能多的排序操作:迟早你会乐意选择这个选择。



I am using GAE and I need to write an Objectify query that asks for the elements created after September 1st.

The dateCreated field is Java.util.Date and it is stored in this format 2012-11-29 16:03:59.494000.

This request is NOT working:

   public List<MyElement> listAllFromUser(String userId)
    {
        Objectify ofy = ObjectifyService.begin();
        Query<MyElement> q=ofy.query(MyElement.class).filter("dateCreated >", "2013-09-01 00:00:00");

        List<MyElement> results = q.list();

        return results;
    }

解决方案

  1. Your column needs to be indexed

    @Indexed protected Date dateCreated;

  2. You can sort only by one column in a request (> and < and != are all considered sorting since the index is sorted, then analyzed)

  3. You can group dates by loosing precision: 2012-11-29 16:03:59.494000 becomes 2012-11-29 this way, you can use == safely. Create a column for each precision you want (day, week, month). In general, try relying the least possible on sorting operations: sooner or later you will be happy for that choice

这篇关于对象查询:按日期过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆