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

Wrong behaviour HAS ONLY query for MongoDB #810

Closed
JPBergsma opened this issue May 21, 2021 · 2 comments · Fixed by #861
Closed

Wrong behaviour HAS ONLY query for MongoDB #810

JPBergsma opened this issue May 21, 2021 · 2 comments · Fixed by #861
Assignees
Labels
bug Something isn't working transformer/MongoDB Related to filter transformer for MongoDB

Comments

@JPBergsma
Copy link
Contributor

In test_mongo.py I noticed the following test:

assert self.transform('elements HAS ONLY "H","He","Ga","Ta"') == { "elements": {"$all": ["H", "He", "Ga", "Ta"], "$size": 4} }
I
This suggests that "elements" has to contain all the elements "H", "He", "Ga", "Ta".
The OPTIMADE definition however is: " list HAS ONLY values: matches if all elements in list are equal to at least one value.
This suggest that {"elements": ["H", "He", "Ga"] should also be a valid.

@JPBergsma JPBergsma added bug Something isn't working transformer/MongoDB Related to filter transformer for MongoDB labels May 21, 2021
@JPBergsma JPBergsma self-assigned this May 21, 2021
@ml-evs ml-evs changed the title Wrong behaviour HAS ANY query for MongoDB Wrong behaviour HAS ONLY query for MongoDB May 22, 2021
@ml-evs
Copy link
Member

ml-evs commented May 22, 2021

Good spot, thanks. The spec also states:

If both list and values do not contain duplicate values, this implements the <= set operator.

which is a handy summary.

HAS ONLY is an optional construct and I think optimade-python-tools is one of the few implementations to support it. This means we can decide to disable it, if it is going to be too arduous to support. Do you have any ideas on how to fix this? We could generate all possible subqueries (e.g. `{"$all": ["H"], "$size": 1} or {"$all": ["H", "He"], "$size": 2}... etc.) but this will get expensive for bigger queries and we might want to discourage it.

@JPBergsma
Copy link
Contributor Author

I think this query selects all documents where all elements are in the specified array ( ["H","He"] ).
db.newtest.find({elements: {"$not": {"$elemMatch": {"$nin" : ["H","He"] }}}})
This query will however also pass documents with empty arrays. So an extra check may be necessary for this.

There is also $setIsSubset which would be more elegant. I however did not yet manage to create a proper query with this keyword. Perhaps it can't handle arrays with null values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working transformer/MongoDB Related to filter transformer for MongoDB
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants