pythonmongodbmongokit

MongoKit Schemaless


I have an object like follows:

@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)

I have a user already in the database entered by hand that does not have the first_name and last_name field. Problem is when i try to run this

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

I get this

>>> 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']

I would like to have some objects that dont have all the fields in the structure. What am I doing wrong?

Added an answer.


Solution

  • It was a bug. Fixed in 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)