MongoDB is a very powerful database, and connecting it to Budibase is done with a few clicks.
Before we get started, make sure you've got the following:
ConnectionStringfor connecting to the database remotely including username and password
- Added the Budibase IP to the Mongo Atlas IP Access List, check Whitelisting for the Budibase IP if you're using the cloud-hosted solution
- The Database name
Now that you have everything you need to hook up your MongoDB installation to Budibase, let's get started
Don't know how to get the ConnectionString? Check this useful guide by MongoDB.
Don't have a Mongo database yet?
The easiest way to get started is by signing up for the (from) free offering from Mongo directly: MongoDB Atlas. We've also used MongoDB Atlas for writing this guide, and testing our integrations!
To connect your MongoDB Data SourcesData Sources - Budibase is built up of multiple data sources, this can be the internal Budibase DB, but can also be your remote MySQL, PostgreSQL, REST or one of the many other sources available in Budibase., head over to the data section in the Budibase UI. Then, click the
+ icon in the left-hand settings panel to add a new data source.
You will be presented a dialog, in which you choose
MongoDB. Then you will be presented with the connection configuration.
Paste your ConnectionString, and enter the database name which you want to connect with, and click
Save and continue to query.
Pressing this button will save the ConnectionString into your Budibase installation.
Connect to MongoDB Atlas
Connecting to a hosted instance of MongoDB Atlas should be straightforward, but do check the useful guide on connecting to a database
Now we're at the fun part. Now that you have added the connection to your database, you can start querying your data to use it straight away in your application.
The first step is to click the "add query button, which is located on the data source page you just added. If you're not on this page, click on the MongoDB connection in the list of data sources on the left.
There are several steps involved in setting up a query.
Configuring your query is essential in making sure it runs correctly. In the table below you'll find what each field means.
The name you want to give the query, this is a visual name. The name is used when selecting a Data SourcesData Sources - Budibase is built up of multiple data sources, this can be the internal Budibase DB, but can also be your remote MySQL, PostgreSQL, REST or one of the many other sources available in Budibase. inside the design section
The action you want to perform. This is a MongoDB specified function. Choosing a function influences the options you get offered to you for actionTypes
Which level do you want to be able to access this query?
The MongoDB collection you want to query
The action-type defined by MongoDB, based on the Function you've selected.
What do the action types mean?
The action types are defined by MongoDB. You can read more about those in the MongoDB Documentation
Bindings in Queries allow you to insert data when using the query. You can use HandleBar Bindings inside the query. A simple, but common, example of a binding would be an ObjectID. This allows you to query a single ID based on a binding you've specified, even from the UI of your application.
Let's take a look at an example where we pass the ID from a binding into the query. Our binding looks like this.
Then, adjusting our query, we add the
id using handlebars directly into the string
Now, wherever you use the created query as a data source in a Data Provider, you can configure the bindings directly from there, using the gearwheel behind the provider selection
This will then open up the configuration modal, where you can bind the value, for example using a Form input.
The fields section in the MongoDB query page, is where you enter the query. The query is expected to be a
JSON Object as defined by the MongoDB documentation. As an example, we're looking at a simple query just fetching based on ID from the database.
Stringify the Queries
If you take a look at the MongoDB documentation, and you find queries there, they're most likely not stringified. As the
Fieldsbox only supports JSON objects, you should convert it to strings.
The transformer is intended for you to have a layer between the data received from your MongoDB database, and how it ends up in your interface. By default you
return the data directly received, but manipulating, or restructuring, data in the transformer can be very useful to be able to limit what is returned, or to make it more eye-friendly.
To understand how Transformers work, read about them on the Transformers page
Now that you have configured the entire query correctly, it's time to save the query. You can't save the query without running it first so we know it actually is working.
So the first thing you want to do now, is click the
Run Query button
As soon as you click the button, the query will be executed. When the query is successful, the result will be displayed below. There you can inspect the result, configure the schema and preview it in a table.
Configuring the schema is important for Budibase, as it allows the interface to understand what kind of data it is dealing with. In order for you to configure the data, click the
Schema Tab, which is next to the
JSON tab in the results section. You should then be presented with a page like this:
Updated about 2 months ago