namlook / mongokit

MongoKit framework try to keep its simplicity when you manage mongodb in python. MongoKit was developed to be fast and light with KISS and DRY in mind. MongoKit brings structured schema and validation layer on top of the great pymongo driver. Discuss with us on Google group : http://groups.google.com/group/mongokit or follow the news on Twitter: http://twitter.com/namlook
http://namlook.github.com/mongokit/
Other
677 stars 132 forks source link

use_dot_notation = True causes InvalidDocument exception #245

Open hchilbule opened 9 years ago

hchilbule commented 9 years ago

Mongokit causes a weird issue when I have a document structure below. I think the issue started when I added 3 new fields which are key-value type dictionary.

structure = {
        'strategy_idx': unicode,
        'strategy_type': IS(u"static", u"normal"),
        'strategy_state': IS(u"draft", u"active", u"suspended"),
        'strategy_size_params': {
            unicode: OR(unicode, int, float)
        },
        'strategy_exec_params': {
            unicode: OR(unicode, int, float)
        },
        'strategy_risk_params': {
            unicode: OR(unicode, int, float)
        },
    }

I was able to insert a new record but when I tried to update the same object and call save(), I got an exception : InvalidDocument | Cannot encode object: {u'FillSize': 20, u'MinSize': 10}

The whole document is below if you want to check for its validity. I tested it using bson.BSON.encode(obj) and it works perfectly.

{u'strategy_size_params': {u'FillSize': 20, u'MinSize': 10}, 
 u'strategy_type': u'static', 
 u'strategy_exec_params': {u'Max': 100, u'Avg': 40}, 
 u'strategy_idx': u'TEST-77753-ID', 
 u'strategy_risk_params': {u'Kind': u'A', u'StopLoss': -60}, 
 u'_id': ObjectId('5582668f75124f311344eb64'), 
 u'strategy_state': u'active'}

This error is with MongoKit 0.9.1.1

mongokit==0.9.1.1
pymongo==2.7

This error went away when I set 'use_dot_notation = False' It took me long time to figure out that this issues is not with the document but with its handling.

Related issue > https://stackoverflow.com/questions/22049076/mongokit-nested-structure-save-error/22078430#

hchilbule commented 9 years ago

I notice that https://github.com/namlook/mongokit/issues/173 is related to this.