1

I am using the pymongo library in Python to update specific entries in my MongoDB, spesifically using the db.update_one() function which was previously working with the aggregation pipeline $set but now fails when using $addFields. I am trying to insert the entry: 'Client Facing': ['No'] into the object:

{
    'Content Format': ['Presentation'], 
    'Category': ['Systems w/TPS'], 
    'Offering Market': ['Enterprise Linux Market'], 
    'Offering Portfolio': ['Enterprise Linux'], 
    'Offering Name': ['HANA excl L Systems - POWER9 H924 Scale-Out'], 
    'Keywords': ['SAP'], 
    'Language': ['English'], 
    'Additional Information': ['prepare-atl'], 
    'Organization Owner': ['Technology'], 
    'Brand': ['Cognitive Systems'], 
    'Content Owner Group': ['Technology - Systems']
}

So I was using the update_one() with the parameters: {'seismic_id': 12345} (this is the filter), and

{'$addFields': {'seismic_properties_obj.Client Facing': ['No']}}

However, when I do this it fails with the error:

pymongo.errors.WriteError: Unknown modifier: $addFields. Expected a valid update modifier or pipeline-style update specified as an array, full error: {'index': 0, 'code': 9, 'errmsg': 'Unknown modifier: $addFields. Expected a valid update modifier or pipeline-style update specified as an array'}
3
  • you can use $addFields only in update with aggregation pipeline, can you show your whole query? Commented Jun 17, 2021 at 13:50
  • @turivishal my full query is just the update_one method that looks like: ``` DB['assets'].update_one({'seismic_id': 12345}, {'$addFields': {'seismic_properties_obj.Client Facing': ['No']}}) ``` Commented Jun 17, 2021 at 14:03
  • 1
    as i said in first comnet $addFields is aggregation stage, you can use $set here. Commented Jun 17, 2021 at 14:06

1 Answer 1

1

you can use the following snippet

DB['assets'].update_one(
    {'seismic_id': 12345},
    {'$set':
        {'seismic_properties_obj.Client Facing': 'No'}
    }
)

This will find record seismic_id = 12345, then add property seismic_properties.Client Facing and set it to 'No'

Note: In this case, $addField is not a valid operator since its used within the aggregation framework. Instead using $set allows you to add/update specific keys without updating the entire document.

Sign up to request clarification or add additional context in comments.

Comments

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.