Accessing particular mongo document fields with pymongo

Hi peoples!

I’ve been traversing through Google & Stack Overlow but couldn’t find a solution that works for me - I’m sure it’s a relatively simple thing i’m missing but can’t quite understand where / what’s going wrong (it doesn’t help the troubleshooting that there’s no particular error I can follow)

I’m currently working with a mongodb & attempting to update a nested document, e.g.:

{'EventName': {'UFC London: Volkov v Aspinall': {'Fight1': {'FightMethod': '',
                                                            'FightRound': '',
                                                            'FightWinner': '',
                                                            'FighterCashValue1': '1420',
                                                            'FighterCashValue2': '580',
                                                            'FighterName1': 'Sergei '
                                                            'FighterName2': 'Shamil '
                                                            'FighterOdds1': '1/3',
                                                            'FighterOdds2': '9/4'}

I’m slightly confused how I would access an existing document, like so above - and update the FightMethod, FightRound & FightWinner My current code looks like below:

def mongo_update_fight_results(EventName, i):

	FightCollection = get_fight_event_collection()

	EventDataQuery = {"EventName" : {f'{EventName}' : f'Fight{i}'}}
	UpdateData = { "$set" : {f"EventName.{EventName}.Fight{i}.FightWinner" : 'Sergei Pavlovich'}}
	DocsUpdated = FightCollection.update_one(EventDataQuery, UpdateData)
	print(DocsUpdated.modified_count, "docs updated")
	print(FightCollection.find_one({},{"EventName" : {f'{EventName}' : f'Fight{i}'}}))

However, I get 0 docs updated

…Any tips on how I access & update FightMethod, FightWinner & FightRound from the above document structure?

I’d be willing to bet your query isn’t turning up any matching documents to update. You need to use dot notation for pointing to subfields in queries unless you’re specifying values to match. Your query is basically asking mongo to find a document where "EventName" matches {f'{EventName}' : f'Fight{i}'} in entirety, not just having those parts match.

It’s usually better to store information as values and not as keys. Having the event name data acting as a fieldname makes the document difficult to search. If you have control over the document schema, I would rethink its structure and if it’s necessary to nest things in such a way where the fieldnames are also data values like that.

Fighter = {
'Name': str,
Fight = {
'FighterOne': Fighter,
'FighterTwo': Fighter,
'Winner': str,
Event = {
'EventName': str,
'Fights': list[Fight]

event_query = {'EventName': 'UFC London: Volkov v Aspinall'}
event_update = {'$set': {'Fights.0.Winner': 'Sergei Pavlovich'}}

Alternatively, if you cannot alter the document schema.
You’re not really looking for any specific document values currently, rather you want to find documents that have a specific structure. So you could try using the $exists operator to find documents that have your specific path of nested fields.

query = {f'TopLevel.{SubField}.DeepField': {'$exists': True}}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.