Skip to content
Mar 6

Advanced Obsidian: Dataview Queries

MT
Mindli Team

AI-Generated Content

Advanced Obsidian: Dataview Queries

Obsidian’s core strength lies in linking your thoughts, but its true power is unlocked when you can interrogate your entire vault as a living database. The Dataview plugin transforms your collection of notes into a queryable knowledge graph, allowing you to create dynamic tables, lists, and dashboards that automatically update. By mastering the Dataview Query Language (DQL) and its JavaScript extension, you move from passive note-taking to active knowledge synthesis, enabling you to track projects, surface forgotten connections, and see patterns across your entire thinking ecosystem.

Understanding the Dataview Foundation

Before writing a single query, you must understand what Dataview can see. It interprets each note in your vault as an object with properties. The most basic property is the note’s frontmatter, a YAML block at the top of a note defined by three dashes (---). Here, you can define custom metadata like project-status: active or book-rating: 5. Dataview also automatically infers implicit properties from the note itself, such as its file.name, file.ctime (creation time), and any tags you’ve used (like #project).

A query is simply a request for a subset of notes that meet certain conditions, presented in a specific format. All queries are written in a dedicated Dataview code block. You must install the Dataview community plugin and enable it in your settings before you can begin. The simplest query lists all notes with a specific tag, providing an instant, auto-updating index.

LIST FROM #project

Crafting Basic Queries: LIST, TABLE, and TASK

The LIST command outputs a bullet-point list of linked note names. It’s useful for quick indexes. For richer data presentation, the TABLE command is indispensable. It allows you to define columns, each pulling from a note property.

TABLE author, rating, status FROM #book SORT rating DESC

This query would find all notes tagged #book, create a table with three columns, and sort the results by the rating field in descending order. The TASK view is specialized for querying Markdown checkboxes (- [ ]). It can aggregate open tasks from across your vault, perfect for a unified task manager.

TASK FROM #project WHERE !completed GROUP BY file.link

The WHERE clause filters results (here, showing only incomplete tasks), and GROUP BY organizes them under their source note. Mastering these three views—LIST, TABLE, and TASK—forms the backbone of most practical Dataview applications.

Mastering Filters, Sorting, and Data Types

To write precise queries, you need robust filtering with the WHERE clause. You can filter on any property. Dataview handles different data types, and your syntax must match. For text (strings), use quotes: WHERE author = "Frank Herbert". For numbers: WHERE rating >= 4. For dates, you can use comparison operators like WHERE file.cday >= date(today) - dur(7 days) to find notes created in the last week.

Functions like date(), dur(), and contains() are vital tools. For example, to find project notes where the status is not "completed": WHERE status != "completed". To find notes where a list field contains a specific value: WHERE contains(genres, "Science Fiction"). After filtering, use SORT to order your results by any field, ascending (ASC) or descending (DESC).

You can also perform inline calculations in a TABLE view. For a table of books with a computed "value score," you might write: TABLE rating, (rating / page-count) AS "Value Score". This creates a new column on the fly by dividing two existing numerical fields, demonstrating how queries can derive new insights.

Creating Dynamic Inline Queries and Custom Fields

Not every query needs its own code block. Inline queries allow you to embed a single data point directly in your note's prose. The syntax uses a single backtick and a DV inline prefix. For instance, you could write: ` = this.file.ctime to dynamically display the current note's creation date. Or, to reference a field from another note: = [[Book Note]].rating `.

This becomes incredibly powerful when combined with custom fields created through frontmatter or inline field declarations. Inline fields are defined directly in your note's content using a key:: value syntax. For example, within a book note, you could write: "The pacing is excellent book-pacing:: fast." Dataview will recognize book-pacing as a field with the value "fast." This lets you add metadata contextually without switching to the frontmatter block, making data entry fluid and natural. Your queries can then filter and sort using these inline fields just like frontmatter.

Extending Power with DataviewJS

While DQL is powerful, some logic is beyond its scope. DataviewJS unlocks the full potential by allowing you to write JavaScript queries within a dataviewjs code block. This gives you programmatic access to your vault data, enabling complex data manipulation, custom rendering, and integration with external libraries.

A simple DataviewJS block might look like this:

const pages = dv.pages("#project").where(p => p.status === "active"); for (let group of pages.groupBy(p => p.quarter)) { dv.header(3, group.key); dv.list(group.rows.file.link); }

This script fetches active project pages, groups them by a custom quarter field, and creates a header and list for each group. With DataviewJS, you can build interactive HTML elements, create complex charts, or perform multi-step data processing. It’s the gateway to building truly bespoke, intelligent dashboards that behave like custom applications within your vault.

Common Pitfalls

  1. Incorrect Data Types in Filters: A common error is filtering a number as text. Writing WHERE rating = "4" will fail if rating is defined as 4 (a number) in your frontmatter. Always ensure your filter's data type matches the field's type—use WHERE rating = 4 for numbers.
  2. Misunderstanding the Query Scope: The FROM clause dictates which notes are queried. A query without a FROM clause runs over your entire vault, which can be slow. Be specific: use FROM #tag or FROM "Folder" to limit scope and improve performance.
  3. Overlooking Missing Fields: If you TABLE a column like priority and some notes lack this field, they will show null in the table. This can clutter results. Use a filter like WHERE priority to show only notes where the field exists, or use the COALESCE() function to provide a default value: TABLE COALESCE(priority, "None").
  4. Static Mindset with Dashboards: The most powerful dashboards are living documents. A pitfall is creating a beautiful query once and forgetting it. Regularly review and refine your queries and metadata schema (your fields and tags) as your vault grows and your needs evolve. The goal is a system that adapts with you.

Summary

  • The Dataview plugin treats your Obsidian vault as a queryable database, allowing you to dynamically aggregate information using the Dataview Query Language (DQL) in code blocks.
  • Master the three core data views: LIST for simple indexes, TABLE for rich metadata display, and TASK for aggregating checkboxes, using WHERE to filter and SORT to organize results.
  • Enhance flexibility with inline queries (` = ... ) for single data points and **inline fields** (key:: value`) for adding metadata contextually within note content.
  • For ultimate control and complex logic, DataviewJS allows you to write JavaScript to manipulate, group, and display your vault data in virtually any format, enabling the creation of complex, interactive dashboards.
  • Effective use requires a consistent metadata strategy through frontmatter and tags, and involves avoiding common pitfalls like type mismatches and overly broad query scopes.

Write better notes with AI

Mindli helps you capture, organize, and master any subject with AI-powered summaries and flashcards.