Connect a MongoDB resource
Learn how to connect your MongoDB database to Retool.
Connecting to MongoDB
Retool supports connecting to both managed and self-hosted MongoDB databases, including MongoDB Atlas Clusters and AWS DocumentDB. Retool uses version 4.1 of the MongoDB Node driver.
Connecting to MongoDB Atlas Clusters
You'll need to get a connection string for connecting to your cluster in the Atlas UI by going through the cluster connect flow. Note the placeholder database name myFirstDatabase
in the example below: it needs to be updated to point to a real collection name in your cluster.
To connect Retool to the MongoDB cluster above, specify host cluster0.qch9r.mongodb.net
with the DNS seed list connection format. Your database name is the name of the collection you want to query. Make sure you've provisioned database and network access first!
Picking a connection format
Retool supports MongoDB's standard connection format (
mongo://
) as well as the new seed list connection format (mongodb+srv://
). If you're using a managed offering such as MongoDB Atlas, the seed list connection format may be preferred.
Whitelisting Retool's IP
If you're using a managed MongoDB database such as MongoDB Atlas, you'll need to add the Retool app to your IP access list. If you're using the cloud version of Retool, the list of Retool IP addresses can be found here.
Querying MongoDB
Supported methods
Retool supports the following MongoDB methods:
find
findOne
findOneAndUpdate
count
distinct
aggregate
insertOne
updateOne
deleteOne
insertMany
updateMany
deleteMany
listCollections
Basic Queries
Write MongoDB queries from Retool's editor interface. Select the method to query your database with, the collection you want to use the query with, and you're good to go.
Below is an example of querying a MongoDB database for companies where name = 'Uber'
We don't support MongoDB's classic syntax
Instead we support MongoDB's Extended JSON syntax. For more information see https://docs.mongodb.com/manual/reference/mongodb-extended-json-v1/
Here's an example of using MongoDB's Extended JSON Syntax to query based on an ObjectId
See below for some simple equivalencies from the normal MongoDB syntax and the Extended JSON Syntax. In Retool, use the Extended JSON Syntax.
// ObjectIds
// supported: Extended JSON syntax
{
_id: {
$oid: "5b3ddc6dd2ab742125322cf3";
}
}
// not supported: MongoDB classic syntax
{
_id: ObjectId("5b3ddc6dd2ab742125322cf3");
}
// Dates
// supported: Extended JSON syntax
{
createdAt: {
$date: "2018-08-31T05:26:05+00:00";
}
}
// not supported: MongoDB classic syntax
{
createdAt: new Date("2018-08-31T05:26:05+00:00");
}
Dynamic Queries
Just like any other datasource in Retool, you can also refer to dynamic variables inside your MongoDB Queries. Here's an example of searching a MongoDB database based on a value of a textinput:
A walkthrough tutorial for working with MongoDB in Retool
If you're working with MongoDB data in Retool, we put together a walkthrough guide to help get your Atlas setup connected and build a sample listing approval tool using Mongo's
sample_airbnb
dataset. Check it out here!
Updated 18 days ago