Skip to main content
Skip to main content

mongodb Table Function

Allows SELECT queries to be performed on data that is stored on a remote MongoDB server.

Syntax

mongodb(host:port, database, collection, user, password, structure[, options[, oid_columns]])

Arguments

ArgumentDescription
host:portMongoDB server address.
databaseRemote database name.
collectionRemote collection name.
userMongoDB user.
passwordUser password.
structureThe schema for the ClickHouse table returned from this function.
optionsMongoDB connection string options (optional parameter).
oid_columnsComma-separated list of columns that should be treated as oid in the WHERE clause. _id by default.
Tip

If you are using the MongoDB Atlas cloud offering please add these options:

'connectTimeoutMS=10000&ssl=true&authSource=admin'

You can also connect by URI:

mongodb(uri, collection, structure[, oid_columns])
ArgumentDescription
uriConnection string.
collectionRemote collection name.
structureThe schema for the ClickHouse table returned from this function.
oid_columnsComma-separated list of columns that should be treated as oid in the WHERE clause. _id by default.

Returned value

A table object with the same columns as the original MongoDB table.

Examples

Suppose we have a collection named my_collection defined in a MongoDB database named test, and we insert a couple of documents:

db.createUser({user:"test_user",pwd:"password",roles:[{role:"readWrite",db:"test"}]})

db.createCollection("my_collection")

db.my_collection.insertOne(
    { log_type: "event", host: "120.5.33.9", command: "check-cpu-usage -w 75 -c 90" }
)

db.my_collection.insertOne(
    { log_type: "event", host: "120.5.33.4", command: "system-check"}
)

Let's query the collection using the mongodb table function:

SELECT * FROM mongodb(
    '127.0.0.1:27017',
    'test',
    'my_collection',
    'test_user',
    'password',
    'log_type String, host String, command String',
    'connectTimeoutMS=10000'
)

or:

SELECT * FROM mongodb(
    'mongodb://test_user:[email protected]:27017/test?connectionTimeoutMS=10000',
    'my_collection',
    'log_type String, host String, command String'
)