librelist archives

« back to archive

[flask] Sorting / order_by with pymongo

[flask] Sorting / order_by with pymongo

From:
Delilah Parks
Date:
2012-06-12 @ 17:23
I need help with sorting. I am using pymongo. I am able to output the
values however the sorting is not working. Please help:

list_of_ques = list()
    for ques in
ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
1}).sort("questions.order", pymongo.DESCENDING):
        list_of_ques.append(ques)
        print list_of_ques

---------------------------- questions.order outputs the following:
[{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'}, {u'order':
u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
----------------------------- Also, Is there an alternative to doing it
this way? I feel like I missing something simple. :) I need for it to out
put

[{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'}, {u'order':
u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]

Thanks

Re: [flask] Sorting / order_by with pymongo

From:
lynadge
Date:
2012-06-12 @ 18:22
I think you don't need a new list.

''' news = news_collection.find().sort('created', -1)

This worked for me. 'created' is a time.time() field.

In your way you create a new list from the sorted list and the new one 
isn't sorted, i think.

Am 12.06.2012 18:23, schrieb Delilah Parks:
> I need help with sorting. I am using pymongo. I am able to output the
> values however the sorting is not working. Please help:
>
> list_of_ques = list()
>     for ques in
> 
> ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
> 1}).sort("questions.order", pymongo.DESCENDING):
>          list_of_ques.append(ques)
>         print list_of_ques
>
> ---------------------------- questions.order outputs the following:
> [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
> {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
>  ----------------------------- Also, Is there an alternative to doing
> it this way? I feel like I missing something simple. :) I need for it
> to out put
>
> [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
> {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
>
> Thanks

Re: [flask] Sorting / order_by with pymongo

From:
Delilah Parks
Date:
2012-06-12 @ 19:12
Thank you, lynadge... I am not trying the following:
u = camp_collection.find().sort("questions.order", -1)
print u

But it is now displaying:
<pymongo.cursor.Cursor object at #######>

???

On Tue, Jun 12, 2012 at 2:22 PM, lynadge <lynadge@barfooz.de> wrote:

> I think you don't need a new list.
>
> ''' news = news_collection.find().sort('created', -1)
>
> This worked for me. 'created' is a time.time() field.
>
> In your way you create a new list from the sorted list and the new one
> isn't sorted, i think.
>
> Am 12.06.2012 18:23, schrieb Delilah Parks:
> > I need help with sorting. I am using pymongo. I am able to output the
> > values however the sorting is not working. Please help:
> >
> > list_of_ques = list()
> >     for ques in
> >
> >
> ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
> > 1}).sort("questions.order", pymongo.DESCENDING):
> >          list_of_ques.append(ques)
> >         print list_of_ques
> >
> > ---------------------------- questions.order outputs the following:
> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
> > {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
> >  ----------------------------- Also, Is there an alternative to doing
> > it this way? I feel like I missing something simple. :) I need for it
> > to out put
> >
> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
> > {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
> >
> > Thanks
>
>

Re: [flask] Sorting / order_by with pymongo

From:
lynadge
Date:
2012-06-12 @ 19:21
Try to iterate over it.

for item in u:
     print item

Am 12.06.2012 20:12, schrieb Delilah Parks:
> Thank you, lynadge... I am not trying the following:
> u = camp_collection.find().sort("questions.order", -1)
> print u
>
> But it is now displaying:
> <pymongo.cursor.Cursor object at #######>
>
>  ???
>
> On Tue, Jun 12, 2012 at 2:22 PM, lynadge <lynadge@barfooz.de [1]>
> wrote:
>
>> I think you don't need a new list.
>>
>> ''' news = news_collection.find().sort('created', -1)
>>
>> This worked for me. 'created' is a time.time() field.
>>
>> In your way you create a new list from the sorted list and the new
>> one
>> isn't sorted, i think.
>>
>> Am 12.06.2012 18:23, schrieb Delilah Parks:
>> > I need help with sorting. I am using pymongo. I am able to output
>> the
>> > values however the sorting is not working. Please help:
>> >
>> > list_of_ques = list()
>> >     for ques in
>> >
>> >
>>
> 
> ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
>> > 1}).sort("questions.order", pymongo.DESCENDING):
>> >          list_of_ques.append(ques)
>> >         print list_of_ques
>> >
>> > ---------------------------- questions.order outputs the
>> following:
>> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
>> > {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
>> >  ----------------------------- Also, Is there an alternative to
>> doing
>> > it this way? I feel like I missing something simple. :) I need
>> for it
>> > to out put
>> >
>> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
>> > {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
>> >
>> > Thanks
>
>
>
> Links:
> ------
> [1] mailto:lynadge@barfooz.de

Re: [flask] Sorting / order_by with pymongo

From:
Delilah Parks
Date:
2012-06-13 @ 15:25
I am still unable to sort. is there r another way to do this... or could
someone provide me some working code.

On Tue, Jun 12, 2012 at 3:21 PM, lynadge <lynadge@barfooz.de> wrote:

> Try to iterate over it.
>
> for item in u:
>     print item
>
> Am 12.06.2012 20:12, schrieb Delilah Parks:
> > Thank you, lynadge... I am not trying the following:
> > u = camp_collection.find().sort("questions.order", -1)
> > print u
> >
> > But it is now displaying:
> > <pymongo.cursor.Cursor object at #######>
> >
> >  ???
> >
> > On Tue, Jun 12, 2012 at 2:22 PM, lynadge <lynadge@barfooz.de [1]>
> > wrote:
> >
> >> I think you don't need a new list.
> >>
> >> ''' news = news_collection.find().sort('created', -1)
> >>
> >> This worked for me. 'created' is a time.time() field.
> >>
> >> In your way you create a new list from the sorted list and the new
> >> one
> >> isn't sorted, i think.
> >>
> >> Am 12.06.2012 18:23, schrieb Delilah Parks:
> >> > I need help with sorting. I am using pymongo. I am able to output
> >> the
> >> > values however the sorting is not working. Please help:
> >> >
> >> > list_of_ques = list()
> >> >     for ques in
> >> >
> >> >
> >>
> >
> >
> ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
> >> > 1}).sort("questions.order", pymongo.DESCENDING):
> >> >          list_of_ques.append(ques)
> >> >         print list_of_ques
> >> >
> >> > ---------------------------- questions.order outputs the
> >> following:
> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
> >> > {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
> >> >  ----------------------------- Also, Is there an alternative to
> >> doing
> >> > it this way? I feel like I missing something simple. :) I need
> >> for it
> >> > to out put
> >> >
> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
> >> > {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
> >> >
> >> > Thanks
> >
> >
> >
> > Links:
> > ------
> > [1] mailto:lynadge@barfooz.de
>
>

Re: [flask] Sorting / order_by with pymongo

From:
Darren Worrall
Date:
2012-06-13 @ 15:36
When you're asking pymongo to sort, you're asking it to sort the
_documents_ by key specified, not return the documents _with the value
of that key sorted_. Is that where the confusion lies?


On 13 June 2012 16:25, Delilah Parks <delilahtp@gmail.com> wrote:
> I am still unable to sort. is there r another way to do this... or could
> someone provide me some working code.
>
> On Tue, Jun 12, 2012 at 3:21 PM, lynadge <lynadge@barfooz.de> wrote:
>>
>> Try to iterate over it.
>>
>> for item in u:
>>     print item
>>
>> Am 12.06.2012 20:12, schrieb Delilah Parks:
>> > Thank you, lynadge... I am not trying the following:
>> > u = camp_collection.find().sort("questions.order", -1)
>> > print u
>> >
>> > But it is now displaying:
>> > <pymongo.cursor.Cursor object at #######>
>> >
>> >  ???
>> >
>> > On Tue, Jun 12, 2012 at 2:22 PM, lynadge <lynadge@barfooz.de [1]>
>> > wrote:
>> >
>> >> I think you don't need a new list.
>> >>
>> >> ''' news = news_collection.find().sort('created', -1)
>> >>
>> >> This worked for me. 'created' is a time.time() field.
>> >>
>> >> In your way you create a new list from the sorted list and the new
>> >> one
>> >> isn't sorted, i think.
>> >>
>> >> Am 12.06.2012 18:23, schrieb Delilah Parks:
>> >> > I need help with sorting. I am using pymongo. I am able to output
>> >> the
>> >> > values however the sorting is not working. Please help:
>> >> >
>> >> > list_of_ques = list()
>> >> >     for ques in
>> >> >
>> >> >
>> >>
>> >
>> >
>> > ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
>> >> > 1}).sort("questions.order", pymongo.DESCENDING):
>> >> >          list_of_ques.append(ques)
>> >> >         print list_of_ques
>> >> >
>> >> > ---------------------------- questions.order outputs the
>> >> following:
>> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
>> >> > {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
>> >> >  ----------------------------- Also, Is there an alternative to
>> >> doing
>> >> > it this way? I feel like I missing something simple. :) I need
>> >> for it
>> >> > to out put
>> >> >
>> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
>> >> > {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
>> >> >
>> >> > Thanks
>> >
>> >
>> >
>> > Links:
>> > ------
>> > [1] mailto:lynadge@barfooz.de
>>
>

Re: [flask] Sorting / order_by with pymongo

From:
Delilah Parks
Date:
2012-06-13 @ 17:53
Darren, I am a complete noob when it comes to this ... could you provide me
with an example. How would I get it to return the desired order / values?

On Wed, Jun 13, 2012 at 11:36 AM, Darren Worrall <darren@iweb.co.uk> wrote:

> When you're asking pymongo to sort, you're asking it to sort the
> _documents_ by key specified, not return the documents _with the value
> of that key sorted_. Is that where the confusion lies?
>
>
> On 13 June 2012 16:25, Delilah Parks <delilahtp@gmail.com> wrote:
> > I am still unable to sort. is there r another way to do this... or could
> > someone provide me some working code.
> >
> > On Tue, Jun 12, 2012 at 3:21 PM, lynadge <lynadge@barfooz.de> wrote:
> >>
> >> Try to iterate over it.
> >>
> >> for item in u:
> >>     print item
> >>
> >> Am 12.06.2012 20:12, schrieb Delilah Parks:
> >> > Thank you, lynadge... I am not trying the following:
> >> > u = camp_collection.find().sort("questions.order", -1)
> >> > print u
> >> >
> >> > But it is now displaying:
> >> > <pymongo.cursor.Cursor object at #######>
> >> >
> >> >  ???
> >> >
> >> > On Tue, Jun 12, 2012 at 2:22 PM, lynadge <lynadge@barfooz.de [1]>
> >> > wrote:
> >> >
> >> >> I think you don't need a new list.
> >> >>
> >> >> ''' news = news_collection.find().sort('created', -1)
> >> >>
> >> >> This worked for me. 'created' is a time.time() field.
> >> >>
> >> >> In your way you create a new list from the sorted list and the new
> >> >> one
> >> >> isn't sorted, i think.
> >> >>
> >> >> Am 12.06.2012 18:23, schrieb Delilah Parks:
> >> >> > I need help with sorting. I am using pymongo. I am able to output
> >> >> the
> >> >> > values however the sorting is not working. Please help:
> >> >> >
> >> >> > list_of_ques = list()
> >> >> >     for ques in
> >> >> >
> >> >> >
> >> >>
> >> >
> >> >
> >> >
> ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
> >> >> > 1}).sort("questions.order", pymongo.DESCENDING):
> >> >> >          list_of_ques.append(ques)
> >> >> >         print list_of_ques
> >> >> >
> >> >> > ---------------------------- questions.order outputs the
> >> >> following:
> >> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
> >> >> > {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
> >> >> >  ----------------------------- Also, Is there an alternative to
> >> >> doing
> >> >> > it this way? I feel like I missing something simple. :) I need
> >> >> for it
> >> >> > to out put
> >> >> >
> >> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
> >> >> > {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
> >> >> >
> >> >> > Thanks
> >> >
> >> >
> >> >
> >> > Links:
> >> > ------
> >> > [1] mailto:lynadge@barfooz.de
> >>
> >
>

Re: [flask] Sorting / order_by with pymongo

From:
Darren Worrall
Date:
2012-06-14 @ 08:07
You dont/cant ask the server to sort those values - it gives you the
list and you sort it yourself:

doc_with_list = collection.find_one({'_id': ObjectId('someid')})
sorted_list = sorted(doc.get('list_of_things_to_sort'))

If you want them stored in a particular order in the database you need
to sort that list before inserting/updating.

On 13 June 2012 18:53, Delilah Parks <delilahtp@gmail.com> wrote:
> Darren, I am a complete noob when it comes to this ... could you provide me
> with an example. How would I get it to return the desired order / values?
>
>
> On Wed, Jun 13, 2012 at 11:36 AM, Darren Worrall <darren@iweb.co.uk> wrote:
>>
>> When you're asking pymongo to sort, you're asking it to sort the
>> _documents_ by key specified, not return the documents _with the value
>> of that key sorted_. Is that where the confusion lies?
>>
>>
>> On 13 June 2012 16:25, Delilah Parks <delilahtp@gmail.com> wrote:
>> > I am still unable to sort. is there r another way to do this... or could
>> > someone provide me some working code.
>> >
>> > On Tue, Jun 12, 2012 at 3:21 PM, lynadge <lynadge@barfooz.de> wrote:
>> >>
>> >> Try to iterate over it.
>> >>
>> >> for item in u:
>> >>     print item
>> >>
>> >> Am 12.06.2012 20:12, schrieb Delilah Parks:
>> >> > Thank you, lynadge... I am not trying the following:
>> >> > u = camp_collection.find().sort("questions.order", -1)
>> >> > print u
>> >> >
>> >> > But it is now displaying:
>> >> > <pymongo.cursor.Cursor object at #######>
>> >> >
>> >> >  ???
>> >> >
>> >> > On Tue, Jun 12, 2012 at 2:22 PM, lynadge <lynadge@barfooz.de [1]>
>> >> > wrote:
>> >> >
>> >> >> I think you don't need a new list.
>> >> >>
>> >> >> ''' news = news_collection.find().sort('created', -1)
>> >> >>
>> >> >> This worked for me. 'created' is a time.time() field.
>> >> >>
>> >> >> In your way you create a new list from the sorted list and the new
>> >> >> one
>> >> >> isn't sorted, i think.
>> >> >>
>> >> >> Am 12.06.2012 18:23, schrieb Delilah Parks:
>> >> >> > I need help with sorting. I am using pymongo. I am able to output
>> >> >> the
>> >> >> > values however the sorting is not working. Please help:
>> >> >> >
>> >> >> > list_of_ques = list()
>> >> >> >     for ques in
>> >> >> >
>> >> >> >
>> >> >>
>> >> >
>> >> >
>> >> >
>> >> > ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
>> >> >> > 1}).sort("questions.order", pymongo.DESCENDING):
>> >> >> >          list_of_ques.append(ques)
>> >> >> >         print list_of_ques
>> >> >> >
>> >> >> > ---------------------------- questions.order outputs the
>> >> >> following:
>> >> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
>> >> >> > {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
>> >> >> >  ----------------------------- Also, Is there an alternative to
>> >> >> doing
>> >> >> > it this way? I feel like I missing something simple. :) I need
>> >> >> for it
>> >> >> > to out put
>> >> >> >
>> >> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
>> >> >> > {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
>> >> >> >
>> >> >> > Thanks
>> >> >
>> >> >
>> >> >
>> >> > Links:
>> >> > ------
>> >> > [1] mailto:lynadge@barfooz.de
>> >>
>> >
>
>

Re: [flask] Sorting / order_by with pymongo

From:
Zach Williams
Date:
2012-06-13 @ 15:30
Here's some flask-pymongo examples that might help you out:
https://github.com/dcrosta/flask-pymongo/blob/master/examples/wiki/wiki.py

On Wed, Jun 13, 2012 at 8:25 AM, Delilah Parks <delilahtp@gmail.com> wrote:

> I am still unable to sort. is there r another way to do this... or could
> someone provide me some working code.
>
>
> On Tue, Jun 12, 2012 at 3:21 PM, lynadge <lynadge@barfooz.de> wrote:
>
>> Try to iterate over it.
>>
>> for item in u:
>>     print item
>>
>> Am 12.06.2012 20:12, schrieb Delilah Parks:
>> > Thank you, lynadge... I am not trying the following:
>> > u = camp_collection.find().sort("questions.order", -1)
>> > print u
>> >
>> > But it is now displaying:
>> > <pymongo.cursor.Cursor object at #######>
>> >
>> >  ???
>> >
>> > On Tue, Jun 12, 2012 at 2:22 PM, lynadge <lynadge@barfooz.de [1]>
>> > wrote:
>> >
>> >> I think you don't need a new list.
>> >>
>> >> ''' news = news_collection.find().sort('created', -1)
>> >>
>> >> This worked for me. 'created' is a time.time() field.
>> >>
>> >> In your way you create a new list from the sorted list and the new
>> >> one
>> >> isn't sorted, i think.
>> >>
>> >> Am 12.06.2012 18:23, schrieb Delilah Parks:
>> >> > I need help with sorting. I am using pymongo. I am able to output
>> >> the
>> >> > values however the sorting is not working. Please help:
>> >> >
>> >> > list_of_ques = list()
>> >> >     for ques in
>> >> >
>> >> >
>> >>
>> >
>> >
>> ques_collection.find({"sitename":sitename,"campurl":campurl},{"questions":
>> >> > 1}).sort("questions.order", pymongo.DESCENDING):
>> >> >          list_of_ques.append(ques)
>> >> >         print list_of_ques
>> >> >
>> >> > ---------------------------- questions.order outputs the
>> >> following:
>> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'2'},
>> >> > {u'order': u'3'}, {u'order': u'1'}, {u'order': u'4'}]}]
>> >> >  ----------------------------- Also, Is there an alternative to
>> >> doing
>> >> > it this way? I feel like I missing something simple. :) I need
>> >> for it
>> >> > to out put
>> >> >
>> >> > [{u'_id': ObjectId('######'), u'questions': [{u'order': u'4'},
>> >> > {u'order': u'3'}, {u'order': u'2'}, {u'order': u'1'}]}]
>> >> >
>> >> > Thanks
>> >
>> >
>> >
>> > Links:
>> > ------
>> > [1] mailto:lynadge@barfooz.de
>>
>>
>