Skip to content

Commit 563095f

Browse files
authored
Merge pull request #4311 from ClickHouse/doc-fix
mongo: improve documentation to handle nested object
2 parents 75b1ba4 + 25b80d6 commit 563095f

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

docs/integrations/data-ingestion/clickpipes/mongodb/quickstart.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ FROM t1;
110110
└─────────────────────────┴────────────────────────────────┘
111111
```
112112

113+
When querying _nested object fields_ using dot syntax, make sure to add the [`^`](https://clickhouse.com/docs/sql-reference/data-types/newjson#reading-json-sub-objects-as-sub-columns) operator:
114+
115+
```sql title="Query"
116+
SELECT _full_document.^shipping as shipping_info FROM t1;
117+
```
118+
119+
```shell title="Result"
120+
┌─shipping_info──────────────────────────────────────┐
121+
│ {"city":"Seattle","cost":19.99,"method":"express"} │
122+
└────────────────────────────────────────────────────┘
123+
```
124+
113125
### Dynamic type {#dynamic-type}
114126

115127
In ClickHouse, each field in JSON has `Dynamic` type. Dynamic type allows ClickHouse to store values of any type without knowing the type in advance. You can verify this with the `toTypeName` function:
@@ -218,9 +230,7 @@ SELECT
218230
CAST(_full_document.status, 'String') AS status,
219231
CAST(_full_document.total_amount, 'Decimal64(2)') AS total_amount,
220232
CAST(parseDateTime64BestEffortOrNull(_full_document.order_date, 3), 'DATETIME(3)') AS order_date,
221-
CAST(_full_document.shipping.method, 'String') AS shipping_method,
222-
CAST(_full_document.shipping.city, 'String') AS shipping_city,
223-
CAST(_full_document.shipping.cost, 'Decimal64(2)') AS shipping_cost,
233+
_full_document.^shipping AS shipping_info,
224234
_full_document.items AS items
225235
FROM t1 FINAL
226236
WHERE _peerdb_is_deleted = 0;
@@ -236,9 +246,7 @@ This view will have the following schema:
236246
│ status │ String │
237247
│ total_amount │ Decimal(18, 2) │
238248
│ order_date │ DateTime64(3) │
239-
│ shipping_method │ String │
240-
│ shipping_city │ String │
241-
│ shipping_cost │ Decimal(18, 2) │
249+
│ shipping_info │ JSON │
242250
│ items │ Dynamic │
243251
└─────────────────┴────────────────┘
244252
```
@@ -250,7 +258,7 @@ SELECT
250258
customer_id,
251259
sum(total_amount)
252260
FROM v1
253-
WHERE shipping_city = 'Seattle'
261+
WHERE shipping_info.city = 'Seattle'
254262
GROUP BY customer_id
255263
ORDER BY customer_id DESC
256264
LIMIT 10;
@@ -272,9 +280,7 @@ CREATE TABLE flattened_t1 (
272280
`status` String,
273281
`total_amount` Decimal(18, 2),
274282
`order_date` DateTime64(3),
275-
`shipping_method` String,
276-
`shipping_city` String,
277-
`shipping_cost` Decimal(18, 2),
283+
`shipping_info` JSON,
278284
`items` Dynamic
279285
)
280286
ENGINE = ReplacingMergeTree()
@@ -289,9 +295,7 @@ SELECT
289295
CAST(_full_document.status, 'String') AS status,
290296
CAST(_full_document.total_amount, 'Decimal64(2)') AS total_amount,
291297
CAST(parseDateTime64BestEffortOrNull(_full_document.order_date, 3), 'DATETIME(3)') AS order_date,
292-
CAST(_full_document.shipping.method, 'String') AS shipping_method,
293-
CAST(_full_document.shipping.city, 'String') AS shipping_city,
294-
CAST(_full_document.shipping.cost, 'Decimal64(2)') AS shipping_cost,
298+
_full_document.^shipping AS shipping_info,
295299
_full_document.items AS items
296300
FROM t1 FINAL
297301
WHERE _peerdb_is_deleted = 0;
@@ -304,7 +308,7 @@ SELECT
304308
customer_id,
305309
sum(total_amount)
306310
FROM flattened_t1
307-
WHERE shipping_city = 'Seattle'
311+
WHERE shipping_info.city = 'Seattle'
308312
GROUP BY customer_id
309313
ORDER BY customer_id DESC
310314
LIMIT 10;

0 commit comments

Comments
 (0)