[Python] mongomock BulkOperationBuilder.add_update() unexpected keyword argument 'sort'

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm testing a function that performs a bulk upsert using UpdateOne with bulk_write. In production (using the real MongoDB client) everything works fine, but when running tests with mongomock I get this error:

    app.mongodb.exceptions.CatalogException: failure in mongo repository function `batch_upsert_catalog_by_sku`: BulkOperationBuilder.add_update() got an unexpected keyword argument 'sort'


    I don't pass any sort parameter in my code. Here's the relevant function:

    def batch_upsert_catalog_by_sku(self, items: List[CatalogBySkuWrite]) -> None:
    operations = []
    current_time = datetime.datetime.now(datetime.timezone.utc)
    for item in items:
    update_fields = item.model_dump()
    update_fields["updated_at"] = current_time

    operations.append(
    UpdateOne(
    {"sku": item.sku, "chain_id": item.chain_id},
    {
    "$set": update_fields,
    "$setOnInsert": {"created_at": current_time},
    },
    upsert=True,
    )
    )

    if operations:
    result = self.collection.bulk_write(operations)
    logger.info("Batch upsert completed",
    matched=result.matched_count,
    upserted=result.upserted_count,
    modified=result.modified_count)


    Has anyone seen this error with mongomock? Is it a version issue or a bug, and what would be a good workaround? I'm using mongomock version 4.3.0 and pymongo version 4.11.

    Thanks!

    Continue reading...

Compartilhe esta Página