diff --git a/product_docs/docs/mongo_data_adapter/5/06_features_of_mongo_fdw.mdx b/product_docs/docs/mongo_data_adapter/5/06_features_of_mongo_fdw.mdx index 7bd716a8264..3b3b39cd336 100644 --- a/product_docs/docs/mongo_data_adapter/5/06_features_of_mongo_fdw.mdx +++ b/product_docs/docs/mongo_data_adapter/5/06_features_of_mongo_fdw.mdx @@ -96,3 +96,61 @@ Steps for retrieving the document: { "_id" : { "$oid" : "58a1ebbaf543ec0b9054585a" }, "warehouse_id" : 2, "warehouse_name" : "Laptop", "warehouse_created" : { "$date" : 1447229590000 } } (2 rows) ``` + +## Accessing nested fields + +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. +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. +You can retrieve these fields from a collection as shown in the following example: + +### Example + +```text +db1> db.test_sub_json.find() +[ + { + _id: ObjectId('658040214890799d6e0173d0'), + key1: 'hello', + key2: { + subkey21: 'hello-sub1', + subkey22: 'hello-sub2', + subtstmp: ISODate('2022-12-16T19:16:17.801Z') + } + } +] +``` + +Steps for retrieving sub-fields from the document: + +1. Create a foreign table. To access a sub-field use the dot (".") in the column name as shown below: + + ```sql + CREATE FOREIGN TABLE ft_nested_json_test( + _id NAME, + key1 varchar, + "key2.subkey21" varchar, + "key2.subkey22" varchar, + "key2.subtstmp" timestamp + )SERVER mongo_server + OPTIONS (database 'db1', collection 'test_sub_json'); + ``` + +1. Retrieve the document with sub-fields: + + ```sql + SELECT * FROM ft_nested_json_test; + __OUTPUT__ + _id | key1 | key2.subkey21 | key2.subkey22 | key2.subtstmp + --------------------------+-------+---------------+---------------+------------------------ + 658040214890799d6e0173d0 | hello | hello-sub1 | hello-sub2 | 16-DEC-22 19:16:17.801 + ``` + +1. Retrieve an individual field: + + ```sql + SELECT "key2.subkey21" FROM ft_nested_json_test; + __OUTPUT__ + key2.subkey21 + --------------- + hello-sub1 + ``` \ No newline at end of file