pymongo没有插入完整的词典

时间:2016-03-17 04:08:13

标签: python mongodb

我试图将嵌套文档插入mongoDB。但是,文档的某些部分缺失。

代码

def setup_user(db, name):
    collection = 'user'
    u = user()
    u.user_name = name
    u.defaultMembership()
    print u.to_dict()
    db[collection].insert(u.to_dict())

打印:

{'prefer_complexity': '_not_available_',
'prefer_color': '_not_available_',
'user_name': 'admin',
'prefer_size': '_not_available_',
'user_membership_funcs': {
    'color': {
        'inf_1': {
            'rel_pos_1': {
                'a': 1,
                'length': 3,
                'b': 1,
                'name': 'Ok',
                'upper_bound': 1,
                'lower_bound': 0,
                'c': 0,
                'eqn': '1/(1 + math.pow(abs(({x} - {c})/{a}), 2*{b}))',
                'type': 'bell function',
                'pos': 1
            },
            'rel_pos_2': {
                'a': 1,
                'length': 3,
                'b': 1,
                'name': 'Like',
                'upper_bound': 1,
                'lower_bound': 0,
                'c': 1,
                'eqn': '1/(1 + math.pow(abs(({x} - {c})/{a}), 2*{b}))',
                'type': 'bell function',
                'pos': 2
            },
            'rel_pos_0': {
                'a': 1,
                'length': 3,
                'b': 1,
                'name': 'Dislike',
                'upper_bound': 1,
                'lower_bound': 0,
                'c': 0,
                'eqn': '1/(1 + math.pow(abs(({x} - {c})/{a}), 2*{b}))',
                'type': 'bell function',
                'pos': 0
            },
            'fuzzy_var': {
                'lower_bound': 0,
                'upper_bound': 1,
                'possible_ratings': OrderedDict([('Dislike', 0), ('Ok', 1), ('Like', 2)])
            }
        }
    }
}

}

但是只有这个插入mongo。

{
"_id" : ObjectId("56ea2a48e53607168bc4b73e"),
"prefer_complexity" : "_not_available_",
"user_membership_funcs" : {
    "color" : {
        "inf_1" : {
            "fuzzy_var" : {
                "lower_bound" : 0,
                "upper_bound" : 1,
                "possible_ratings" : {
                    "Dislike" : 0,
                    "Ok" : 1,
                    "Like" : 2
                }
            }
        }
    },
"prefer_color" : "_not_available_",
"user_name" : "admin",
"prefer_size" : "_not_available_"
}

如您所见,我错过了rel_pos_#信息。我不太确定出了什么问题。

1 个答案:

答案 0 :(得分:0)

解决。当第二次调用to_dict()时,返回了另一个dict。与我使用的发电机有关。