• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

127
Views
AWS DocumentDB Syntax differs from MongoDB for UpdateMany

I'm attempting to perform an update using a substr after migrating from mongo > documentDB, however I am getting a strange error.

The following works on Mongo 4.0 using pymongo:

    await db[cls.colname].update_many(
        {'$or': [{'preview_title': {'$exists': False}}, {'preview_title': None}]},
        [{'$set': {'preview_title': {'$substr': ['$title', 0, 70]}}}],
    )

The equivalent mongo shell command also works:

db.getCollection('pages').updateMany({'$or': [{'preview_title': {'$exists': false}}, {'preview_title': null}]}, [{'$set': {'preview_title': {'$substrBytes': ['$title', 0, 70]}}}])

However, after switching to AWS DocumentDB, it doesn't accept the update as an array or it will return this error: "MongoError: Wrong type for parameter u" image

If I change the command to the below, it will work (basically just remove the square brackets from the update portion):

db.getCollection('pages').updateMany({'$or': [{'preview_title': {'$exists': false}}, {'preview_title': null}]}, {'$set': {'preview_title': {'$substr': ['$title', 0, 70]}}})

This would be fine, however, the equivalent pymongo call doesnt seem to work:

await db[cls.colname].update_many(
            {'$or': [{'preview_title': {'$exists': False}}, {'preview_title': None}]},
            {'$set': {'preview_title': {'$substr': ['$title', 0, 70]}}},
        )

It returns the following error: pymongo.errors.WriteError: Document can't have $ prefix field names: $substr

over 3 years ago · Santiago Trujillo
1 answers
Answer question

0

Passing array as the update is called "updating with an aggregation pipeline". DocumentDB apparently doesn't implement that (among a bunch of other MongoDB features).

When you remove the array from update you change from using https://docs.mongodb.com/manual/reference/operator/aggregation/set/ to using https://docs.mongodb.com/manual/reference/operator/update/set/. You'll notice that the update $set doesn't accept expressions like aggregation $set does. So your shell update with $set writes a (nested) document {'$substr': ['$title', 0, 70]} into the preview field. Generally keys starting with dollars are not queryable because the dollar prefix is used by MongoDB operators, so writing such a document into a field is a bad idea. Pymongo tells you this and refuses the operation. The shell allows it because it's also used internally by MongoDB server engineers for testing and so it permits various weird/unusual things to be done.

over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error