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

Match array query doesn't work the same as Mongo #17

Open
mattbishop opened this issue May 12, 2021 · 8 comments
Open

Match array query doesn't work the same as Mongo #17

mattbishop opened this issue May 12, 2021 · 8 comments

Comments

@mattbishop
Copy link

Mongo has the ability to search for a specific array value, meaning arrays that have the same elements in the same order:

https://docs.mongodb.com/manual/tutorial/query-arrays/#match-an-array

If I run a similar query with mongo-query-to-postgres-jsonb, I get back results that have all of the array elements, but the ordering is irrelevant. Given the Mongo tutorial example, if I have the same data in jsonb tables, then issue this query:

const sql = mongoToPostgres("inventory", { tags: ["red", "blank"] }, ["tags"]);

Instead of getting back a single result, 4 rows are returned, which is the behaviour for the $all operator. I would expect four results for this query:

const sql = mongoToPostgres("inventory", { tags: { "$all": ["red", "blank"] } }, ["tags"]);
@mattbishop
Copy link
Author

I don't think Postgres is going to be able to implement the same 'exactly this array' semantics: https://www.postgresql.org/docs/13/datatype-json.html#JSON-CONTAINMENT

@thomas4019
Copy link
Owner

Ahh, that's interesting. It does look like using the containment operator won't work, but it should be possible using the equality operator, right? Perhaps when matching arrays with arrays, we could change it to not use the the containment. I can investigate more this weekend.

@mattbishop
Copy link
Author

mattbishop commented May 13, 2021

I am messing about with how to handle queries like { “an-array-field”: “$elemMatch”: { “$gte”: 4, “$le”: 6 } } and am thinking generating JSONPath queries will be better, since the containment and other operators cannot do this sort of comparison. Reading through https://www.postgresql.org/docs/13/functions-json.html Section "The SQL/JSON Path language"

@mattbishop
Copy link
Author

@thomas4019 do you have a minimum Postgres version you want to support?

@thomas4019
Copy link
Owner

I’m not opinionated on the version. @dannyzaken @romayalon Do you have an opinion about what PostreSQL versions to support?

Depending on the case, it could make sense to the the version as a parameter so the library can do the best possible with the features available

@dannyzaken
Copy link
Contributor

@thomas4019 currently we are using PostgreSQL 12

@mattbishop
Copy link
Author

My attempts to use JSON Path mixed with other JSON operators has failed. I don't think it's possible to mix the two.

Might be interesting to consider refactoring this lib to use JSON Path exclusively.

@thomas4019
Copy link
Owner

@mattbishop Thanks for the update! Can you share an example of a query using both?

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

3 participants