1

I created indices on the text fields, the following query runs fine in Mongo.

cursor = db.collection.find({ "$text" : { "$search": "stack" } },{ "score": { "$meta": "textScore" } }).sort( {'score' : {'$meta': 'textScore'} }).skip(0).limit(4)

But when I run it using PyMongo, I get the following error:

TypeError: if no direction is specified, key_or_list must be an instance of list

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
  • 1
    possible duplicate of [sorting with mongodb and python](http://stackoverflow.com/questions/10242149/sorting-with-mongodb-and-python) – Nick T Sep 24 '14 at 18:19
  • not quite a duplicate since text search has slightly different sort syntax. OP: please include the actual code you used that gave the error. It's also a good idea to include versions that you are using. – Asya Kamsky Sep 24 '14 at 22:32
  • @AsyaKamsky this is the actual code, this line in particular gives error, I'm able to run other queries and I'm also able to run this query in mongo db terminal, but unable to run it using pymongo – Kanwal Prakash Singh Sep 25 '14 at 04:52

1 Answers1

4

Take a look at the syntax in pymongo docs for sort.

You don't show what actual syntax you are using in Python, but it should be like the example on that page:

cursor.sort([('score', {'$meta': 'textScore'})])
Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133