MongoKit Schemaless

时间:2011-08-23 09:06:26

标签: python mongodb mongokit

我有一个如下对象:

@connection.register
class User(Document):
    __collection__ = 'users'
    __database__ = 'crucible_projects'
    use_schemaless = True
    structure = {
        'name': unicode,
        'password': unicode,
        'last_name': unicode,
        'first_name': unicode,
        'email': unicode,
        'last_login': datetime.datetime,
    }
    use_dot_notation = True
    def __repr__(self):
        return '<User %r>' % (self.name)

我手边输入的用户已经没有first_name和last_name字段。问题是我试图运行这个

def login_user(user):
    found_attribute = connection.User.find_one({'name':user})
    found_attribute.last_login = datetime.datetime.utcnow()
    found_attribute.save()

我明白了

>>> import db_users
>>> db_users.login_user('admin')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "db_users.py", line 91, in login_user
  found_attribute.save()
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\document.py", line 394, in save
  self.validate(auto_migrate=False)
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\document.py", line 243, in validate
  super(Document, self).validate()
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 353, in validate
  self._validate_doc(self, self.structure)
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 569, in _validate_doc
  "missed fields : %s" % struct_doc_diff )
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 524, in _raise_exception

raise exception(message)
mongokit.schema_document.StructureError: missed fields : ['first_name', 'last_name']

我想要一些没有结构中所有字段的对象。我做错了什么?

添加了答案。

1 个答案:

答案 0 :(得分:1)

这是一个错误。修正于v0.7.1

Changelog :

* change MongokitMasterSlaveConnection to MasterSlaveConnection for consistency
* fix #57 -- support pymongo > 1.9 in grid.py
* fix #45 -- remove automatique index creation
* fix #43 -- slicing a cursor should return a mongokit document, not dict
* Dont try to convert None struct to json (patch from @mLewisLogic thanks !)
* fix schemaless issue (thanks to Mihai Pocorschi for reporting it)