how to order query by separate field

1 view
Skip to first unread message

Baron

unread,
Oct 29, 2009, 8:53:51 PM10/29/09
to Google App Engine
Hello,

I have a model with over 1000 records and I want to select the top N
records on field A that satisfy conditions on field B. (For example
the youngest N records with a certain name.)

My concern is if I only filter on field B and order in code
afterwards, then I will miss some records not in the top 1000.

I was hoping to create an index so that the results are always ordered
by field A, but that doesn't seem to be possible.
Ideas?

ryan baldwin

unread,
Oct 29, 2009, 9:01:07 PM10/29/09
to google-a...@googlegroups.com
I believe you can create an index that sorts by youngest, then by name, and you should be good.  Assuming your "youngest" field is called "createdDate".  

In app.yaml:

- kind: MyModel
  properties:
    - name: createdDate
      direction: asc

If I understand what I just typed, that will create an index on the MyModel entity, ordering first by createdDate ascending then by A ascending.  You could, I believe, then do the following:

MyModel.all().filter=('A =', 123).order(createdDate).fetch(limit=20)

That being said, I'm not sure if DataStore actually guarantees the order of results... I assume so...?

- ryan.

ryan baldwin

unread,
Oct 29, 2009, 9:02:00 PM10/29/09
to google-a...@googlegroups.com
In my example I left out the sorts by A ascending because I later realised it may not be necessary... I believe, however, that my index does sort by createdDate ascending... I haven't written one in a long, long time, so my apologies. ;)

- ryan.

Baron

unread,
Nov 1, 2009, 6:48:24 PM11/1/09
to Google App Engine
I found that if I used my query a few times on the development server
then your above code snippet was automatically inserted in my
index.yaml - neat! (not app.yaml)
Then when I uploaded to Google App Engine I got errors for a few
minutes while the index was building, but eventually it worked.


On Oct 30, 12:02 pm, ryan baldwin <ryanbald...@gmail.com> wrote:
> In my example I left out the sorts by A ascending because I later realised
> it may not be necessary... I believe, however, that my index *does* sort by
> createdDate ascending... I haven't written one in a long, long time, so my
> apologies. ;)
>
> - ryan.
>
> On Thu, Oct 29, 2009 at 7:01 PM, ryan baldwin <ryanbald...@gmail.com> wrote:
> > I believe you can create an index that sorts by youngest, then by name, and
> > you should be good.  Assuming your "youngest" field is called "createdDate".
>
> > In app.yaml:
>
> >  - kind: MyModel
> >   properties:
> >     - name: createdDate
> >       direction: asc
>
> > If I understand what I just typed, that will create an index on the MyModel
> > entity, ordering first by createdDate ascending then by A ascending.  You
> > could, I believe, then do the following:
>
> > MyModel.all().filter=('A =', 123).order(createdDate).fetch(limit=20)
>
> > That being said, I'm not sure if DataStore actually guarantees the order of
> > results... I assume so...?
>
> > - ryan.
>
Reply all
Reply to author
Forward
0 new messages