August 10, 2023

How to Check Whether an Inline Fields Contains a Value with Obsidian Dataview

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:

  1. Builds a table that displays the status and topics inline fields as individual columns
  2. Pulls in all notes that are tagged with #type/blog-post
  3. 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.

  1. contains is a helper method (like using SUM in an Excel spreadsheet) that Dataview provides us
  2. 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 notes WHERE 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 notes WHERE the topics inline field contains 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.