Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda