Skip to content

Commit 35cdad6

Browse files
committed
Create mongo-external.md
1 parent a62eff8 commit 35cdad6

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

docs/mongo-external.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# MongoDB External Table
2+
3+
Since Timeplus Enterprise [v2.9](/enterprise-v2.9) and v2.8.2, you can send data to and read data from MongoDB collections via the MongoDB External Table.
4+
5+
## CREATE EXTERNAL TABLE
6+
7+
To create an external table for MongoDB, you can run the following DDL SQL:
8+
9+
```sql
10+
CREATE EXTERNAL TABLE [IF NOT EXISTS] name
11+
(<col_name1> <col_type1>, <col_name2> <col_type2>, ...)
12+
SETTINGS
13+
type = 'mongodb',
14+
uri = 'mongodb://user:pwd@host:port/db?options', -- the MongoDB connection URI the external table read/write data from/to
15+
collection = '' -- the MongoDB collection name
16+
```
17+
For the full list of settings, see the [DDL Settings](#ddl-settings) section.
18+
19+
### Examples
20+
21+
#### Write to Self-Hosting MongoDB
22+
Assuming you have created an index `students` in a deployment of OpenSearch or ElasticSearch, you can create the following external stream to write data to the index.
23+
24+
```sql
25+
CREATE EXTERNAL TABLE mongodb_t1 (
26+
name string,
27+
gpa float32,
28+
grad_year int16
29+
) SETTINGS
30+
type = 'mongodb',
31+
uri = 'mongodb://mongoadmin:mongopasswd@localhost/test?authSource=admin',
32+
collection = 'students'
33+
```
34+
35+
Then you can insert data via a materialized view or just
36+
```sql
37+
INSERT INTO mongodb_t1(name,gpa,grad_year) VALUES('Jonathan Powers',3.85,2025);
38+
```
39+
40+
#### Write to MongoDB Atlas
41+
The MongoDB Atlas by default shows the connection URI in the format `mongodb+srv://<username>:<password>@<cluster-address>/<default-auth-db>`. The `mongodb+srv` protocol is not supported yet. Please use the `mongodb` protocol instead. You can find the connection URI in the MongoDB Atlas UI, and choose C++ driver and 3.1.x as the version. The connection URI will look like this:
42+
43+
```
44+
mongodb://<user>:<db_password>@ac-z64ksma-shard-00-00.v6m8dak.mongodb.net:27017,ac-z64ksma-shard-00-01.v6m8dak.mongodb.net:27017,ac-z64ksma-shard-00-02.v6m8dak.mongodb.net:27017/?ssl=true&replicaSet=atlas-iokbsd-shard-0&authSource=admin&retryWrites=true&w=majority&appName=Cluster0
45+
```
46+
47+
```sql
48+
CREATE EXTERNAL TABLE mongodb_t1 (
49+
name string,
50+
gpa float32,
51+
grad_year int16
52+
) SETTINGS
53+
type = 'mongodb',
54+
uri = 'mongodb://user:thepassword@shard1.id.mongodb.net:27017,shard2.id.mongodb.net:27017,shard3.id.mongodb.net:27017/testdb?ssl=true&replicaSet=setname&authSource=admin&retryWrites=true&w=majority&appName=appName',
55+
collection = 'students'
56+
```
57+
58+
#### Read From MongoDB
59+
You can also read data from MongoDB collections via the external table. The following example reads data from the `students` collection in MongoDB.
60+
61+
```sql
62+
SELECT * FROM mongodb_t1
63+
```
64+
You can also filter the data using a `WHERE` clause or apply aggregations on the data. Timeplus will try best to push down the filter and aggregation operations to the MongoDB, so that only the necessary data is transferred from MongoDB to Timeplus.
65+
### DDL Settings
66+
67+
#### type
68+
The type of the external stream. The value must be `mongodb` to send data to MongoDB.
69+
70+
#### uri
71+
The endpoint of the MongoDB service.
72+
73+
#### collection
74+
The name of the MongoDB collection to read/write data from/to.
75+
76+
#### connection_options
77+
MongoDB connection string options as a URL formatted string. e.g. 'authSource=admin&ssl=true'.
78+
79+
#### oid_columns
80+
A comma-separated list of columns that should be treated as oid in the `WHERE` clause. Default to `_id`.
81+
82+
## DROP EXTERNAL TABLE
83+
84+
```sql
85+
DROP STREAM [IF EXISTS] name
86+
```

0 commit comments

Comments
 (0)