Pymongo$push不更新数组,不例外

2024-09-25 08:29:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有嵌套数组的文档,我似乎无法更新,我一直在兜圈子,它开始困扰我,这是文档:

mongo =  pymongo.Connection()['FINS_ALL_L']['FIN_L']

f =         {'plant': 'local',
            'T1':{
            'PID':4278,
            'INST_SPECS' :{'unit_stk': 6386,
                           'thresh': 0.4,
                           'max_in': 789878,
                           'avg_cut': 45565},
            'PU_ARRAY'   : [{'power': 45789, 'unit': 78},{'power': 45757, 'unit': 1},{'power': 45127, 'unit': 11},{'power': 42567, 'unit': 10}]},

            'T2':{
            'PID':8422,
            'INST_SPECS' :{'unit_stk': 4575,
                           'thresh': 0.49,
                           'max_in': 187878,
                           'avg_cut': 14787},
            'PU_ARRAY'   : [{'power': 51475, 'unit': 7},{'power': 59895, 'unit': 2},{'power': 57578, 'unit': 3},{'power': 54525, 'unit': 15}]}}

    py_mong = mong.find_one({'plant':'local'})['T2']['PU_ARRAY']
    print py_mong

    >>>[{u'power': 51475, u'unit': 7}, {u'power': 59895, u'unit': 2}, {u'power': 57578, u'unit': 3}, {u'power': 54525, u'unit': 15}]

我试过很多不同的'$push',它们不会抛出错误,但似乎也不会更新。例如:

^{2}$

这不会引发异常,但是还没有更新。有人能帮忙吗?在


Tags: in文档localunitarraypidmaxpower
1条回答
网友
1楼 · 发布于 2024-09-25 08:29:02

没有匹配,没有更新。“T2”下面没有这样的“值”,它只是一个“字段名”。我不知道你是否有意$exists,但{a1}并不在乎。此外,语句中也不需要使用positial $运算符,因为即使这是一个$exists测试,也不会匹配数组位置。”T2“不是一个数组:

mongo.update(
   {'plant': 'local'},
   {'$push': { 'T2.PU_ARRAY': {'power': 42577, 'unit': 19 } } }
)

相关问题 更多 >