How to sort mongodb with pymongo
NickName:WildBill Ask DateTime:2011-11-13T10:13:58

How to sort mongodb with pymongo

I'm trying to use the sort feature when querying my mongoDB, but it is failing. The same query works in the MongoDB console but not here. Code is as follows:

import pymongo

from  pymongo import Connection
connection = Connection()
db = connection.myDB
print db.posts.count()
for post in db.posts.find({}, {'entities.user_mentions.screen_name':1}).sort({u'entities.user_mentions.screen_name':1}):
    print post

The error I get is as follows:

Traceback (most recent call last):
  File "find_ow.py", line 7, in <module>
    for post in db.posts.find({}, {'entities.user_mentions.screen_name':1}).sort({'entities.user_mentions.screen_name':1},1):
  File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/cursor.py", line 430, in sort
  File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/helpers.py", line 67, in _index_document
TypeError: first item in each key pair must be a string

I found a link elsewhere that says I need to place a 'u' infront of the key if using pymongo, but that didn't work either. Anyone else get this to work or is this a bug.

Copyright Notice:Content Author:「WildBill」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8109122/how-to-sort-mongodb-with-pymongo

More about “How to sort mongodb with pymongo” related questions

How to sort mongodb with pymongo

I'm trying to use the sort feature when querying my mongoDB, but it is failing. The same query works in the MongoDB console but not here. Code is as follows: import pymongo from pymongo import

Show Detail

How to sort mongodb with pymongo

I'm trying to use the sort feature when querying my mongoDB, but it is failing. The same query works in the MongoDB console but not here. Code is as follows: import pymongo from pymongo import

Show Detail

How to sort mongodb with pymongo

I'm trying to use the sort feature when querying my mongoDB, but it is failing. The same query works in the MongoDB console but not here. Code is as follows: import pymongo from pymongo import

Show Detail

sort by string length in Mongodb/pymongo

I was wondering if anyone knows how to sort a mongodb find() result by string length. I have tried something like db.foo.find().sort({item.lenght:-1}) but obviously doesn't work. Can somebody help...

Show Detail

Pymongo auto sort the input dictionary

I am using Pymongo to access Mongo db. I want to search for all people nearby a specified location with name contains a string. For example, I want to search all people nearby [105.0133, 21.3434] and

Show Detail

PyMongo sort with metadata

I wondering how to convert the follow mongodb query to pymongo syntax db.articles.find( { $text: { $search: "cake" } }, { score: { $meta: "textScore" } } ).sort( { score: { $meta: "textScore...

Show Detail

find documents that mongodb moves with pymongo

I am trying to count in my mongodb profiler data all the documents, that have been moved when they were updated. In the mongo shell I do this via: db.system.profile.find({op:"update", moved:true}).

Show Detail

How do I sort array of dictionaries in Pymongo?

I have this example MongoDB entry in a users DB: { _id: xxx, name: &quot;name&quot;, files:[{ fileName: &quot;test&quot;, size: 50 }, { ...

Show Detail

speed up writing to mongoDB with pymongo

I am working on an API for data from a weather model - not anything that will run in production. I use MongoDB to hold the data. The data are gridded with dimension [Time: X, Latitude: 1100, Longit...

Show Detail

MongoDB sort overflow with PyMongo

I have collection "doc" containing a field called "topic". It is initialized to -1 and when I get the topic, I update the field with the corresponding topic e.g. sports. When I do a query in PyMong...

Show Detail