0

I am having trouble using $push to append a new record to an existing document ID in MongoDB Atlas using Pymongo.

The existing collection in MongoDB Atlas is as follows:

{'_id': ObjectId('60d0a244102671b912874753'),
 'entities': [{'uen': '12345678K',
               'name': 'COMPANY A',
               'persons': [{'personId': 'P123456',
                            'personIdType': 'Passport/Others',
                            'personNationality': 'INDIAN',
                            'personName': 'SOME NAME',
                            'personRole': 'OFFICER',
                            'personAddress': {'address1': 'ADDRESS x'}}]}]}

I am trying to append a new person's information who belongs to the same 'COMPANY A' under the same objectID :

{'personId': '34567F',
 'personIdType': 'Passport/Others',
 'personNationality': 'ABC',
 'personName': 'NEW PERSON',
 'personRole': 'OFFICER',
 'personAddress': {'address1': 'ADDRESS y'}}

So ideally the updated collection will look like:

{'_id': ObjectId('60d0a244102671b912874753'),
 'entities': [{'uen': '12345678K',
               'name': 'COMPANY A',
               'persons': [{'personId': 'P123456',
                            'personIdType': 'Passport/Others',
                            'personNationality': 'INDIAN',
                            'personName': 'SOME NAME',
                            'personRole': 'OFFICER',
                            'personAddress': {'address1': 'ADDRESS x'}},
                           {'personId': '34567F', # Newly appended information
                            'personIdType': 'Passport/Others',
                            'personNationality': 'ABC',
                            'personName': 'NEW PERSON',
                            'personRole': 'OFFICER',
                            'personAddress': {'address1': 'ADDRESS y'}}]}]}

The code I am using does not seem to update anything, which I am unsure why:

toPopulate = {'personId': '34567F',
 'personIdType': 'Passport/Others',
 'personNationality': 'ABC',
 'personName': 'NEW PERSON',
 'personRole': 'OFFICER',
 'personAddress': {'address1': 'ADDRESS y'}}

docID = '60d0a244102671b912874753' 
db.my_collection.update_one({'_id': docID},
                                {'$push': {'entities.persons': toPopulate}})

Is $push the correct method to use? If not, how else can I update my data?


UPDATE: After adopting user199805's suggestion, the update still did not occur. However, it wasn't an issue with the syntax. Instead, I realized the value of docID did not contain the correct value. I initially obtained it using

documentId = []
result = db.my_collection.find({'entities.uen': '12345678K'},
                           {'_id': 1}) 
for i in result:
    documentId.append(i)
docID = str(documentId[0]['_id']) 

Which assigned the id '60d0a244102671b912874753' in string format. This could not be read, and I solved it by removing the str() function, or rather

docID = documentId[0]['_id']

Which gave me ObjectId('60d0a244102671b912874753'). Passing it to the call to update_one solved the problem successfully. Thank you user199805.

Ry123
  • 64
  • 1
  • 5

1 Answers1

0
db.collection.update_one(
  {'_id': docID},
  {'$push': {'entities.$[].persons': toPopulate}})
  • Have a look at Mongodb $push in nested array
  • Use 'entities.$[].persons'. Here is the reference for the $[] operator.
  • Make sure your match condition, {'_id' : docID}, is performing as desired.
user199805
  • 61
  • 5