Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate_is_mapping when UpdateOne criteria is a list #867

Open
lucassimon opened this issue Nov 27, 2023 · 0 comments
Open

validate_is_mapping when UpdateOne criteria is a list #867

lucassimon opened this issue Nov 27, 2023 · 0 comments

Comments

@lucassimon
Copy link

lucassimon commented Nov 27, 2023

Hello, I am trying to update a document but I got this error. Follow above the command.

self._collection.update_one(
                {"taxId": tax_id, "someField": {"$ne": product.lower()}},
                [
                    {
                        "$set": {
                            "someField": {
                                "$ifNull": [
                                    {"$concatArrays": ["$someField", [product.lower()]]},
                                    [product.lower()],
                                ],
                            },
                        }
                    },
                ],
            )

So, when I run this command. I got this exception on mongomock/collections.py

TypeError: update must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping

venv/lib/python3.11/site-packages/mongomock/collection.py:79: TypeError

This function validates the option for update

ipdb> Mapping
<class 'collections.abc.Mapping'>
ipdb> option
'update'
ipdb> value
[{'$set': {'someField': {'$ifNull': [{'$concatArrays': ['$someField', ['foo']]}, ['foo']]}}}]
ipdb> isinstance(value, Mapping)
False

So, i don't know if it is a bug, because when I ran this command in a mongosh its successfully.

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}

How can I do this update correctly, using mongomock and updating the someField field correctly?

  • When the field does not exist someField, it creates a field as an array with value 'foo'.
  • When the field is an array. Append the foo value.
  • When the field is someField:null, change/replace the field as array with value someField:['foo']

And how can I fixes this "bug" to accept my update criteria [{'$set': {'someField': {'$ifNull': [{'$concatArrays': ['$someField', ['foo']]}, ['foo']]}}}]. As you can see I use a list [{$set}...]

@lucassimon lucassimon changed the title validate_is_mapping UpdateOne validate_is_mapping when UpdateOne criteria is a list Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant