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

Pymongo $push not updating array, no exception

我有一个包含嵌套数组的文档,我似乎无法更新,我一直在兜圈子,它开始让我恼火,这是文档:

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”的变体,它们不会抛出错误,但它们似乎也不会更新。例如:

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

这不会抛出异常,但还没有更新。有人可以帮忙吗?

没有匹配,没有更新。 "T2"下面没有这个"value",只是一个"field name"。我不知道您是否打算 $exists$push 不在乎。此外,语句中不需要正位 $ 运算符,因为即使这是 $exists 测试,您甚至不会匹配数组位置。 "T2" 不是数组:

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