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