4

In mongoshell, the below statement is working fine:

db.grades.find({'type':'homework'}).sort({$score:1})

But when I am trying to do same in pymongo, i am facing an error:

itr= collection.find({'type':'homework'}).sort(['score', pymongo.ASCENDING])

Error:

    for (key, value) in index_list:
    ValueError: too many values to unpack

1 Answer 1

8

Yes, this always confuses me too.

You need to pass into sort a list of pairs, and you're passing a list of length 2 (i.e. a pair).

This should work:

itr= collection.find({'type':'homework'}).sort([ ['score', pymongo.ASCENDING] ])

The idea is that you can pass more than one pair in your list (for secondary order, etc.).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.