0

在 Python 中使用 Mongokit。在计算时遇到一些麻烦,请获取最后 n 条记录。不确定 Python 到底想要什么语法,但我有:

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

这给了我错误:

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:\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

任何帮助都会很棒。

4

1 回答 1

1

你应该给它一个[(field, direction), ...]列表,而不是一个字典。

必须订购排序规格;dicts 没有排序。

不确定是否需要使用列表,请尝试(field, direction).

于 2011-06-14T13:02:04.810 回答