Skip to content

$each modifier is not handled correctly in $addToSet command #846

Open

Description

@sumezulike

Using the $each modifier with $addToSet doesn't always result in unique values because of how the values of the $each are handled:

https://github.com/mongomock/mongomock/blob/ca41f27161c50d83d5ef8c899623084dd6f09a1d/mongomock/collection.py#LL704C37-L706C80

Example:

import mongomock

client = mongomock.MongoClient()
coll = client.db.coll
id = coll.insert_one({"numbers": []}).inserted_id

coll.find_one_and_update({"_id": id}, {"$addToSet": {"numbers": {"$each": [1, 1, 1, 1]}}})
print(coll.find_one({"_id": id}))

This will result in {"numbers": [1, 1, 1, 1]} instead of {"numbers": [1]}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions