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

$in operator fails on array fields #18

Closed
Dobatymo opened this issue Oct 1, 2021 · 6 comments
Closed

$in operator fails on array fields #18

Dobatymo opened this issue Oct 1, 2021 · 6 comments

Comments

@Dobatymo
Copy link

Dobatymo commented Oct 1, 2021

According to https://docs.mongodb.com/manual/reference/operator/query/in/#mongodb-query-op.-in

If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (for example, <value1>, <value2>, and so on).

mongita raises an error in this case:

Traceback (most recent call last):
  File "test.py", line 57, in test_mongita
    result = list(col.find({"names: {"$in": ["asd", "qwe"]}}))
  File "Python38\lib\site-packages\mongita\cursor.py", line 56, in __iter__
    for el in self._gen():
  File "Python38\lib\site-packages\mongita\collection.py", line 870, in __find
    for doc_id in self.__find_ids(filter, sort, limit, metadata=metadata):
  File "Python38\lib\site-packages\mongita\collection.py", line 845, in __find_ids
    if doc and _doc_matches_slow_filters(doc, slow_filters):
  File "Python38\lib\site-packages\mongita\collection.py", line 193, in _doc_matches_slow_filters
    if _doc_matches_agg(doc_v, query_ops):
  File "Python38\lib\site-packages\mongita\collection.py", line 143, in _doc_matches_agg
    if doc_v not in query_val:
TypeError: unhashable type: 'list'
@scottrogowski
Copy link
Owner

Just acknowledging that I have seen this. I looked over the code and did some quick tests and it looks like I missed implementing the second part of $in functionality that you mention. That certainly needs to get fixed.

What I could not do was reproduce your TypeError which is a little more concerning to me. Do you have any example documents that will cause that to trigger? Are you using memory or disk?

@scottrogowski
Copy link
Owner

Candidate patch to address $in/$nin behavior - https://github.com/scottrogowski/mongita/pull/21/files

Would still be curious if you have a way to reproduce the TypeError

@Dobatymo
Copy link
Author

Dobatymo commented Oct 4, 2021

@scottrogowski Sorry, my $in parameter was a set. I edited the traceback incorrectly. It should look like this.

result = list(col.find({"names: {"$in": {"asd", "qwe"}}}))

@scottrogowski
Copy link
Owner

scottrogowski commented Oct 4, 2021

Ah, you had me worried there. PyMongo throws an error when you use sets as well. The difference is that it's a bson.errors.InvalidDocument rather than a TypeError. So while I think I'll try to reproduce that exact error, I'm not as worried.

Regardless, you helped me find something I had overlooked with the $in/$nin operators... so thank you.

@Dobatymo
Copy link
Author

Dobatymo commented Oct 4, 2021

Yes sorry I didn't test the code with pymongo. I noticed that sets did work with the standard (non-array field) $in operator so I just extended the code to the array field version and saw the error.

@scottrogowski
Copy link
Owner

Addressed the $in/$nin with V1.1. I'll set aside document validation for sets for the moment in favor of a larger release later.

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

2 participants