Finding data
This section covers how to search for data using constraints
Last updated
This section covers how to search for data using constraints
Last updated
This section takes a long-form look at how searches in Bubble work. To see the the more concise and technical complete list of settings, constraints and operators, you can check out our core reference entry on searches.
Having added and updated data in our database, we'll also need a way search for that data and display it around our app.
Whenever you need to find some data in the database, you can perform a search using the Do a search for . This lets you set up a dynamic query to the database with specific constraints, and Bubble returns all things of that data type that match the constraints. You can only search for one data type at a time, such as Users.
Let's say you want to search for all users who have the name John. We can set up the Do a search for function like the example below.
Data sources like Do a search for are one of the building blocks in dynamic expressions. To read more about how to work with expressions, check out our dedicated article on the subject: Article: Dynamic expressions
First, we set the Type of content to User and in the Data source dropdown we pick Do a search for:
Then we set up a constraint which instructs Bubble to only return users that have the name John. The result will always be a list, even if it contains just one thing.
This is the basic way that database queries work: we tell Bubble what kind of data to search for, and then provide one or more constraints that each thing in the returned list needs to match. We can use this logic to find data in all sorts of scenarios:
To display the results on a screen
As part of a workflow (for example if we want to make changes to one or more things)
As part of a
In the example above, we used a static value as a constraint: the name John. We can also set this up as a dynamic value. For example, we could change the condition to ask for the value of an input field instead. This would let your users type in a name and search the database for users that match:
Element data sources using Do a search for are synchronised with the database in real-time. This means that any changes happening in the database, even if made by another user, will be immediately visible in your app.
There's no need to manually update searches. The exception is if the initial search was fetched in a workflow.
Sometimes you'll want to sort the result in a specific way, such as showing users in alphabetical order or tasks by their priority or deadline. This can be used to display the records in a list, or it can be used to find a specific record. For example, if you want to load the last user that was created in your database, you can sort the list by creation date and isolate the newest user with the first item or last item operator (depending on the sorting order).
You can set the sort order when you perform a search.
In some cases you will be referring to a list that's not loaded from a search, or where for some reason you need to apply the sorting after another operator in the expression. In that case you can use the sorted operator.
Bubble has two ways of handling database records: as single items or as a list. It's important to understand this distinction, because the two are treated differently. For example, when you , a group element will expect a single thing, while a repeating group will expect a list of things.
Whenever you perform a search, the result will be a list – even if it only returns one thing (meaning it's a list with a count of one, but still a list).
Whenever you set up a data source that returns a list, such as Do a search for, Bubble and expression that expect a single item will return an error in the . In cases like this, you will need to instruct Bubble to load one of the records in the list. You do this by adding another operator in the data source expression:
First item will return the first item in the list. What item that will be depends on how the list is sorted. The default sorting is by creation date.
Last item will return the last item in the list. What item that will be depends on how the list is sorted. The default sorting is by creation date.
Random item will return a item from the list.
Item #: will return a single item as specified by its index number in the list. This operator requires you to also define a number, such as Item 5.
By combining search constraints with the first item operator you can often reach the exact thing you were looking for. For example, you could search for users and use the Email field as a constraint. Since emails are unique, you can be sure to find the right record by providing the email address of an existing user. If you then apply the first item to the list of results, you will have turned the list into a single thing.
Sometimes you'll need to go the other way and turn a single item into a list, even if that list still only contains the one record. To do this, use the operator.
Searching for data in the database with Do a search for is one way of finding data.
Data source | Description | Returns |
---|---|---|
Do a search for | Searches the database using constraints. | A list of things |
Current user | The user currently using the app | One thing |
Current page thing | The database record currently loaded onto the page using the Go to page action. | One thing |
Get data from page URL | Getting a thing from a URL parameter that contains a unique ID. | One thing |
Things's X | Getting a thing from a field saved on another thing, such as Current User's Company | A list or a single item, depending on the field |