One of my favorite Obsidian plugins is Obsidian Dataview. It's brilliant at letting me create custom dashboards. However, I recently came across a problem where I struggled to query notes with inline fields properly. And after searching high and low in the forums and docs, I figured out a solution and wanted to share it here.
The Problem
When creating a dashboard, I love leveraging the inline fields to create custom metadata properties that can be queried. It gives a kind of database schema feel as a result.
For example, let's take a blog post note:
# Obsidian After 3 Years of Use
Still lovin' my time with Obsidian.
Type:: #type/blog-post
Topics:: [[Obsidian]]
Status:: #status/draft
Creating a dataview query to check whether an inline field equates to a value
If I wanted to build a custom dataview to see all blog posts that have the topic of [[Obsidian]]
, here is how I might have built a custom dataview:
TABLE status, topics
FROM #type/blog-post
WHERE topics = [[@Obsidian]]
To break down each line, the query:
- Builds a table that displays the
status
andtopics
inline fields as individual columns - Pulls in all notes that are tagged with
#type/blog-post
- Checks to see if the
topics
inline field value is equivalent to [[@Obsidian|Obsidian]]
But wait, what if the field contains other values that's not relevant...
While this works for the example we've setup, there eventually comes a point where you might have a blog post that requires more than one topic that's not a complete match to the original query.
For example, here is a blog post where I might talk about Obsidian and Notion:
# Obsidian vs Notion: The Showdown
As someone who could be considered a Notion power user, here's how my experience between the two has been.
Type:: #type/blog-post
Topics:: [[@Obsidian]], [[Notion]]
Status:: #status/draft
Unfortunately, in this scenario, our Obsidian vs Notion: The Showdown
post won't be included in the our query from above.
So, what do we do from here?
The Solution
The silver bullet that we're looking for is the contains
method.
Here's what it looks like in practice:
TABLE status, topics
FROM #type/blog-post
WHERE contains(topics, [[@Obsidian]])
While the syntax isn't the most intuitive at first glance, here's how I would break it down.
contains
is a helper method (like usingSUM
in an Excel spreadsheet) that Dataview provides us- It takes two parameters which are comma separated and wrapped with parentheses
- Example:
contains(DATA_SOURCE, DESIRED_VALUE)
Now that you know the syntax, think of the method as:
Get notesWHERE
DATA_SOURCE
contains
DESIRED_VALUE
In other words, to solve our problem, we want to check if an inline field contains the desired value we want. Taking our example from above:
WHERE contains(topics, [[@Obsidian]])`
It results in the semantic translation of our query to:
Grab notesWHERE
thetopics
inline fieldcontains
the[[@Obsidian]]
note.
And that's all there is to it!
Next Steps
If you'd like to learn more about how contains
works, check out the the official docs.