1

I have a document with a nested array that I can't seem to update, I have been going around in circles and it's starting to grate on me, here is the doc:

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

I have tried many variants of '$push', they don't throw an error, but they don't seem to update either. For example:

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

This does not throw an exception, but yet, no update. Can someone help?

1 Answer 1

3

No match, no update. There is no such "value" under "T2", it's just a "field name". I don't know if you intended $exists but $push does not care. Also there is no need for the positinal $ operator in the statement as you would not even be matching an array position even if this were an $exists test. "T2" is not an array:

mongo.update(
   {'plant': 'local'},
   {'$push': { 'T2.PU_ARRAY': {'power': 42577, 'unit': 19 } } }
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I should really get my head into the docs, in a way this site is both a blessing and a curse (isn't complaining).
@ajsp As I said, I think you got yourself confused trying to test if "PU_ARRAY" exists under "T2". But "T2" is neither an "array" ( no need for positional match ) nor when you "dot notate" as shown an if the field was no already there yet then a new array field of that name with the entries you specify will be created. Otherwise the entries will be appended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.