开发者

Mongokit - Find Last Added Record

开发者 https://www.devze.com 2023-03-13 07:49 出处:网络
Using Mongokit with Python. Having some trouble working out get the last n number of records. Not sure of the syntax Python wants here exactly, but I have:

Using Mongokit with Python. Having some trouble working out get the last n number of records. Not sure of the syntax Python wants here exactly, but I have:

record = collection.find(sort = [{'timestamp': DESCENDING}],limit=10)

Which gives me the error:

Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\flask-0.6-py2.6.egg\flask\app.py", line 874, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python26\lib\site-packages\flask-0.6-py2.6.egg\flask\app.py", line 864, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python26\lib\site-packages\flask-0.6-py2.6.egg\flask\app.py", line 861, in wsgi_app
    rv = self.dispatch_request()
  File "C:\Python26\lib\site-packages\flask-0.6-py2.6.egg\flask\app.py", line 696, in dispatch_reque
st
    return self.view_functions[rule.endpoint](**req.view_args)
  File "E:\My Dropbox\Cranktrain\Blog\crankblog\apps\admin\views.py", line 24, in panel
    record = collection.find(sort = [{'timestamp': DESCENDING}],limit=10)
  File "C:\Python26\lib\site-packages\mongokit-0.5.13.1-py2.6.egg\mongokit\collection.py", line 67,
in find
    return Cursor(self, *args, **kwargs)
  File "C:开发者_如何学Go\Python26\lib\site-packages\mongokit-0.5.13.1-py2.6.egg\mongokit\cursor.py", line 35, in _
_init__
    super(Cursor, self).__init__(*args, **kwargs)
  File "C:\Python26\lib\site-packages\pymongo-1.9-py2.6-win32.egg\pymongo\cursor.py", line 95, in __
init__
    self.__ordering = sort and helpers._index_document(sort) or None
  File "C:\Python26\lib\site-packages\pymongo-1.9-py2.6-win32.egg\pymongo\helpers.py", line 65, in _
index_document
    for (key, value) in index_list:
ValueError: need more than 1 value to unpack

Any help would be great.


You should give it a [(field, direction), ...] list, not a dict.

Sorting specifications must necessarily be ordered; dicts are not ordered.

Not sure that you need to use a list, try just (field, direction).

0

精彩评论

暂无评论...
验证码 换一张
取 消