python - mongokit 未找到集合

标签 python mongodb mongokit

我将 mongokit 与 Flask 一起使用,每次尝试使用我创建的集合时,我都会收到错误 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.<dbname>.<collection>()作品。是的,确实有这样一个集合。

我错过了什么?

最佳答案

引用tutorial您链接:

To avoid repeating ourselves though, let’s specify the database and collection name in the Document definition:

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

>>> bp = connection.BlogPost()

在 shell 示例中,模型对象是通过 connection 对象构造的。就您而言,您只需执行user = User()。尝试通过您用于注册模型的同一个 connection 实例创建用户(例如 user = connection.User())。

关于python - mongokit 未找到集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12980761/

相关文章:

python - 在 Tensorflow 中如何卡住保存的模型

mongodb - MongoDB有像HeidiSQL或PHPMyAdmin这样的东西吗?

javascript - 使用 Angularjs 和 Mongoose 进行无限滚动 - 性能

javascript - 如何更新嵌套数组 MongoDb Node js 中的必填字段

mongodb - 无法从docker连接到mongodb实例: Connection refused

python - 如何使用 MongoKit 对远程数据库主机进行身份验证?

Python 交换列表元素时出现超时错误(最小交换次数 2)

python - 如果未实现 __lt__ 协议(protocol),比较如何进行?

python - 卡住 Python 代码时缺少多处理模块