Skip to main content
Skip to main content

MongoDB

MongoDB engine is read-only table engine which allows to read data from remote MongoDB collection.

Only MongoDB v3.6+ servers are supported. Seed list(mongodb+srv) is not yet supported.

Note

If you're facing troubles, please report the issue, and try to use the legacy implementation. Keep in mind that it is deprecated, and will be removed in next releases.

Creating a Table

Engine Parameters

  • host:port — MongoDB server address.

  • database — Remote database name.

  • collection — Remote collection name.

  • user — MongoDB user.

  • password — User password.

  • options — MongoDB connection string options (optional parameter).

Tip

If you are using the MongoDB Atlas cloud offering connection url can be obtained from 'Atlas SQL' option. Seed list(mongodb**+srv**) is not yet supported, but will be added in future releases.

Also, you can simply pass a URI:

Engine Parameters

  • uri — MongoDB server's connection URI

  • collection — Remote collection name.

Types mappings

MongoDBClickHouse
bool, int32, int64any numeric type, String
doubleFloat64, String
dateDate, Date32, DateTime, DateTime64, String
stringString, UUID
documentString(as JSON)
arrayArray, String(as JSON)
oidString
binaryString if in column, base64 encoded string if in an array or document
any otherString

If key is not found in MongoDB document (for example, column name doesn't match), default value or NULL (if the column is nullable) will be inserted.

Supported clauses

Only queries with simple expressions are supported (for example, WHERE field = <constant> ORDER BY field2 LIMIT <constant>). Such expressions are translated to MongoDB query language and executed on the server side. You can disable all these restriction, using mongodb_throw_on_unsupported_query. In that case ClickHouse tries to convert query on best effort basis, but it can lead to full table scan and processing on ClickHouse side.

Note

It's always better to explicitly set type of literal because Mongo requires strict typed filters.
For example you want to filter by Date:

This will not work because Mongo will not cast string to Date, so you need to cast it manually:

This applied for Date, Date32, DateTime, Bool, UUID.

Usage Example

Assuming MongoDB has sample_mflix dataset loaded

Create a table in ClickHouse which allows to read data from MongoDB collection:

Query:

Troubleshooting

You can see the generated MongoDB query in DEBUG level logs.

Implementation details can be found in mongocxx and mongoc documentations.