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

Add support for $addToSet with bool value #868

Open
nivhanin opened this issue Dec 12, 2023 · 1 comment
Open

Add support for $addToSet with bool value #868

nivhanin opened this issue Dec 12, 2023 · 1 comment

Comments

@nivhanin
Copy link

The problem arises when we try to use the addToSet function with a boolean field.

In our use case, we have a boolean field 'is_flag'. When we try to use addToSet to ensure that we only have unique values in this field, the mock library does not support this operation. This is inconsistent with the real MongoDB functionality, which does support addToSet on boolean fields, and this inconsistency is impeding our ability to conduct accurate unit tests.

We kindly request that this issue be addressed, as it is critical for our code to function as expected.

Steps to reproduce the issue:

Create a boolean field in a MongoDB document.
Code query example:

pipeline = [
    {"$match": {"my_id": my_id},
    {
        "$group": {
             # We group by 0 to get all the documents in the collection
            # and then we use $addToSet to get the unique values
            "_id": 0,
            "flags": {"$addToSet": "$is_flag"},
         }
    },
]
flags = await myDocument.aggregate(pipeline).to_list(1)

Attempt to use addToSet function on this boolean field.
Expected Outcome: The addToSet function should work on boolean fields, ensuring that there are only unique values in this field. 'flags': [False]
Actual Outcome: The addToSet function does not support boolean fields, causing unexpected behavior returns: 'flags': [None].

We look forward to your help in resolving this issue. Thank you.

@nivhanin
Copy link
Author

I think mongomock not support boolean (although mongo does support it):

elif operator == '$addToSet':
    value = []
    val_it = (val or None for val in values)
    # Don't use set in case elt in not hashable (like dicts).
    for elt in val_it:
        if elt not in value:
            value.append(elt)
    doc_dict[field] = value

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