Skip to content

Commit 51a7df9

Browse files
committed
Update docs about querying arrays and wildcards
1 parent dd4455a commit 51a7df9

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ Cast strings to number when sorting.
8383
* [Mongo Docs](https://docs.mongodb.org/manual/tutorial/query-documents/#match-a-field-without-specifying-array-index)
8484

8585
With MongoDB, you can search a document with a subarray of objects that you want to match when any one of the elements in the array matches.
86-
This tools implements it in SQL using a subquery so it is not be the most efficient.
86+
This tool implements it in SQL using a subquery, so it will likely not be the efficient on large datasets.
8787

88-
Example document.
88+
To enable subfield matching, you can pass a third parameter which is either an array of field names that will be assumed
89+
to potentially be arrays or `true` if you want it to assume any field can be an array.
90+
91+
Example document:
8992
```js
9093
{
9194
"courses": [{
@@ -95,9 +98,17 @@ Example document.
9598
}]
9699
]
97100
```
98-
Example query to match:
101+
Example queries to match:
99102
```js
100103
mongoToPostgres('data', { 'courses.distance': '5K' }, ['courses'])
104+
mongoToPostgres('data', { 'courses.distance': '5K' }, true)
105+
```
106+
107+
This then creates a PostgreSQL query like the following:
108+
```
109+
(data->'courses'->>'distance'='5K'
110+
OR EXISTS (SELECT * FROM jsonb_array_elements(data->'courses')
111+
WHERE jsonb_typeof(data->'courses')='array' AND value->>'distance'='5K'))
101112
```
102113
103114
## Supported Features

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongo-query-to-postgres-jsonb",
3-
"version": "0.2.6",
3+
"version": "0.2.7",
44
"description": "Converts MongoDB queries to postgresql queries for jsonb fields.",
55
"main": "index.js",
66
"directories": {

0 commit comments

Comments
 (0)