【问题标题】:mongokit No collection foundmongokit 未找到集合
【发布时间】:2012-10-19 19:00:56
【问题描述】:

我在烧瓶中使用 mongokit,每次尝试使用我创建的集合时,我都会收到错误 No collection found

我在单独的文件 models.py 中定义了我的集合。它看起来像这样:

from mongokit import Connection, Document
import os
import sys

here = os.path.dirname(os.path.abspath(__file__))
path = os.path.abspath(os.path.join(here, 'settings'))
sys.path.append(path)
from settings import base as settings

connection = Connection()

@connection.register
class Contact(Document):
    __database__ = settings.MONGO_DBNAME
    __collection__ = "Contact"

    structure = {
        "name":unicode,
        "mobile_number":unicode,
    }

    required_fields = ["name"]


@connection.register
class User(Document):
    __database__ = settings.MONGO_DBNAME
    __collection__ = 'User'

    structure = {
        "username":unicode,
        "twitter_access_token":unicode,
        "twitter_token_secret":unicode,
        "contacts":[Contact]
    }
    required_fields = ["username"]
    default_values = {
            "twitter_access_token": "",
            "twitter_token_secret": ""
        }

但后来我尝试了:

>>> from models import User
>>> u = User()
>>> u["username"] = "somename"
>>> u.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 404, in save
    self.validate(auto_migrate=False)
  File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 230, in validate
    (size_limit, size_limit_str) = self._get_size_limit()
  File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 214, in _get_size_limit
    server_version = tuple(self.connection.server_info()['version'].split("."))
  File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 622, in __getattribute__
    raise ConnectionError('No collection found') 
mongokit.mongo_exceptions.ConnectionError: No collection found

我关注了this tutorial,但连符号connection.&lt;dbname&gt;.&lt;collection&gt;() 都不起作用。是的,确实有这样的收藏。

我错过了什么?

【问题讨论】:

    标签: python mongodb mongokit


    【解决方案1】:

    引用您链接的tutorial

    为了避免重复,让我们指定数据库和 文档定义中的集合名称:

    @connection.register
    class BlogPost(Document):
        __collection__ = 'blog_posts'
        __database__ = 'blog'
        structure = {...}
    
    >>> bp = connection.BlogPost()
    

    在shell示例中,模型对象是通过connection对象构造的。在你的情况下,你只是在做user = User()。尝试通过您用于注册模型的同一 connection 实例创建用户(例如 user = connection.User())。

    【讨论】:

    • 并非如此。 connection.User(),以及connection.contacts.User()返回如下错误:Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/pymongo/database.py", line 769, in __call__ self.__name, self.__connection.__class__.__name__)) TypeError: 'Database' object is not callable. If you meant to call the 'User' method on a 'Connection' object it is failing because no such method exists.
    • 也许这是因为模型注册到了与您在 shell 中引用的不同的Connection 实例?我刚刚使用教程创建了一个新脚本,如果连接不同,我能够产生错误。
    猜你喜欢
    • 2019-12-12
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    相关资源
    最近更新 更多