Skip to content

Commit 6c7495f

Browse files
authored
Merge pull request #6350 from EnterpriseDB/DOCS-1142
Added documentation for sub-JSON accessing feature in Mongo FDW Key Features
2 parents f98d2cf + a6d0877 commit 6c7495f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

product_docs/docs/mongo_data_adapter/5/06_features_of_mongo_fdw.mdx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,61 @@ Steps for retrieving the document:
9696
{ "_id" : { "$oid" : "58a1ebbaf543ec0b9054585a" }, "warehouse_id" : 2, "warehouse_name" : "Laptop", "warehouse_created" : { "$date" : 1447229590000 } }
9797
(2 rows)
9898
```
99+
100+
## Accessing nested fields
101+
102+
MongoDB Foreign Data Wrapper allows you to access individual fields within nested JSON documents by mapping the nested structure to columns in a foreign table.
103+
This works by mapping the nested structure of the MongoDB document to relational columns in the foreign table definition, using dot notation (key2.subkey21) to reference nested fields.
104+
You can retrieve these fields from a collection as shown in the following example:
105+
106+
### Example
107+
108+
```text
109+
db1> db.test_sub_json.find()
110+
[
111+
{
112+
_id: ObjectId('658040214890799d6e0173d0'),
113+
key1: 'hello',
114+
key2: {
115+
subkey21: 'hello-sub1',
116+
subkey22: 'hello-sub2',
117+
subtstmp: ISODate('2022-12-16T19:16:17.801Z')
118+
}
119+
}
120+
]
121+
```
122+
123+
Steps for retrieving sub-fields from the document:
124+
125+
1. Create a foreign table. To access a sub-field use the dot (".") in the column name as shown below:
126+
127+
```sql
128+
CREATE FOREIGN TABLE ft_nested_json_test(
129+
_id NAME,
130+
key1 varchar,
131+
"key2.subkey21" varchar,
132+
"key2.subkey22" varchar,
133+
"key2.subtstmp" timestamp
134+
)SERVER mongo_server
135+
OPTIONS (database 'db1', collection 'test_sub_json');
136+
```
137+
138+
1. Retrieve the document with sub-fields:
139+
140+
```sql
141+
SELECT * FROM ft_nested_json_test;
142+
__OUTPUT__
143+
_id | key1 | key2.subkey21 | key2.subkey22 | key2.subtstmp
144+
--------------------------+-------+---------------+---------------+------------------------
145+
658040214890799d6e0173d0 | hello | hello-sub1 | hello-sub2 | 16-DEC-22 19:16:17.801
146+
```
147+
148+
1. Retrieve an individual field:
149+
150+
```sql
151+
SELECT "key2.subkey21" FROM ft_nested_json_test;
152+
__OUTPUT__
153+
key2.subkey21
154+
---------------
155+
hello-sub1
156+
```

0 commit comments

Comments
 (0)