This commit is contained in:
Rico Sta. Cruz 2016-10-16 23:49:05 +08:00
parent 65f2288c42
commit 4b42ffc54e
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
2 changed files with 32 additions and 6 deletions

View File

@ -8,8 +8,34 @@ SELECT * FROM users WHERE data->>'name' = 'John'
SELECT data->>'name' AS name FROM users SELECT data->>'name' AS name FROM users
``` ```
| Postgres | JavaScript pseudocode | | Operator | Example | Description | Returns |
| ---- | ---- | | ---- | ---- | ---- | ---- |
| WHERE `data->>'name'` = 'John' | `data.name` | | `->` | `data->2` | Get array element `2` | JSON |
| WHERE `data ? 'name'` | `data.hasOwnProperty('name')` | | `->` | `data->'name'` | Get object key `name` | JSON |
| `jsonb_array_elements_text(data->'tags')` | `data.map(d => d.tags).flatten.uniq` | | `#>` | `data#>'{a,b}'` | Get keypath `a,b` (eg, `data.a.b`) | JSON |
| ---- | ---- | ---- | ---- |
| `->>` | `data->>2` | Get array element `2` | text |
| `->>` | `data->>'name'` | Get object key `name` | text |
| `#>` | `data#>>'{a,b}'` | Get keypath `a,b` (eg, `data.a.b`) | text |
`>` returns JSON, `>>` returns text.
### Boolean operators
| Operator | Example | Description |
| ---- | ---- | ---- |
| `?` | `data ? 'name'` | Does `data` have key `name`? |
| `?|` | `data ?| array['a','b']` | Does `data` have `a` or `b`? |
| `?&` | `data ?& array['a','b']` | Does `data` have `a` and `b`? |
| `@>` | `'{"a":1,"b":2}'::jsonb @> '{"b":2}'::jsonb` | Does `left` include `right`? |
| `<@` | `'{"a":1}'::jsonb <@ '{"a":1,"b":2}'::jsonb` | Does `right` include `left`? |
When `?`/`?|`/`?&` works on objects, it checks keys; when it works on arrays, it checks for elements.
- `'{"a":1}'::jsonb ? 'a'`
- `'["a"]'::jsonb ? 'a'`
## References
- <https://www.postgresql.org/docs/9.4/static/functions-json.html>
- <https://www.postgresql.org/docs/9.4/static/datatype-json.html>

View File

@ -6,7 +6,7 @@ category: JavaScript libraries
| npm | yarn | | npm | yarn |
| --- | ---- | | --- | ---- |
| `npm install` | `yarn` | | `npm install` | `yarn` |
| `npm install gulp` | `yarn add gulp` | | `npm install gulp --save` | `yarn add gulp` |
| `npm install gulp --save-dev --save-exact` | `yarn add gulp --dev --exact` | | `npm install gulp --save-dev --save-exact` | `yarn add gulp --dev --exact` |
| `npm install -g gulp` | `yarn global add gulp` | | `npm install -g gulp` | `yarn global add gulp` |
| `./node_modules/.bin/gulp` | `yarn run gulp` | | `./node_modules/.bin/gulp` | `yarn run gulp` |