简体   繁体   中英

Objectify queries: filter by date

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM