Finding data
This section covers how to search for data using constraints
Once you've added and updated data in your database, you'll need a way to find that data and display it around your app. This article covers how searching works, from a basic query to sorting, isolating a single record, and the different data sources you can pull from.
Doing a search
Whenever you need to find data in the database, you perform a search using the Do a search for data source. This sets up a dynamic query with specific constraints, and Bubble returns all things of that data type that match. You can search for one data type at a time, such as User.
Say you want to find all users named John. First, set the Type of content to User, then pick Do a search for in the Data source dropdown.
Then add a constraint that tells Bubble to return only users whose name is John. The result is always a list, even when it contains just one thing.

This is how database queries work at their core: you tell Bubble what kind of data to search for, then give it one or more constraints that each thing in the returned list has to match. You can use a search in all sorts of places:
To display results on a screen
As part of a workflow, for example to make changes to one or more things
As part of a conditional expression
Static and dynamic constraints
In the example above, the constraint was a static value: the name John. You can also set a constraint as a dynamic value, so it changes based on something in your app.
For example, instead of a fixed name, you could point the constraint at the value of an input field. That lets your users type a name and search the database for matching users.
Display name
By default, a dynamic expression shows as the expression itself, such as Do a search for. You can optionally give it a more descriptive label, like "Search for John". The label doesn't change how the search works, it just helps keep your app organized and easy to review.

To change the display name, click on the expression and edit the field Display name.
Sorting the results
Sometimes you'll want to sort the results a particular way, like showing users alphabetically or tasks by priority or deadline. Sorting is useful both for displaying a list and for finding one specific record. For example, to load the last user created in your database, you can sort the list by creation date and then isolate the newest one with the first item or last item operator, depending on the sort order.
Sorting on the search directly
You can set the sort order directly when you perform the search.

Sorting with an operator
In some cases you'll be working with a list that isn't loaded from a search, or you'll need to apply sorting after another operator in the expression.
For those cases, use the sorted operator:

Lists versus single things
Bubble handles database records in two ways: as a single item, or as a list. Understanding this distinction matters, because the two are treated differently. When you load data into container elements, for example, a group expects a single thing, while a repeating group or table (web) or mobile lists expects a list.
A search always returns a list, even when only one thing matches. In that case it's a list with a count of one, but it's still a list.
Loading a single record from a list
When a data source returns a list, like Do a search for does, any Bubble element or operator that expects a single item will flag an error in the issue tracker. To resolve this, you tell Bubble to load just one record from the list by adding an operator to the expression.
First item
The first item in the list. Which one that is depends on how the list is sorted (by creation date, by default).
Last item
The last item in the list. Which one that is depends on how the list is sorted (by creation date, by default).
Random item
A pseudorandom item from the list.
Item #
A single item at the index number you specify, such as Item 5. This operator requires you to enter a number.
Combining search constraints with the first item operator often gets you to the exact thing you're after. For example, you could search users with the Email field as a constraint. Since emails are unique, providing an existing user's email address returns the right record. Applying first item to that result turns the list into a single thing.
Turning a single record into a list
Sometimes you'll need to go the other way, turning a single item into a list, even if that list holds only the one record. To do this, use the converted to list operator.
This comes up whenever something in your app expects a list but what you have is a single thing.
Other data sources
Searching with Do a search for is one way to find data, but not the only one. Depending on what you need, another data source may get you there more directly.
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 record currently loaded onto the page with the Go to page action.
One thing
Get data from page URL
A thing retrieved from a URL parameter that contains a unique ID.
One thing
Thing's X
A value stored in a field on another thing, such as Current User's Company.
A list or a single item, depending on the field
Other ways to learn
Last updated
Was this helpful?