Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation for sub-JSON accessing feature in Mongo FDW Key Features #6350

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Loading