Dataview Query for number of files with specific *INLINE* metadata parameter

Things I have tried

  • Looked through the forum and found this post and this other post which showed how to query for the total number of files with a YAML parameter.
  • Tested the inline query with YAML:
    • --- hub: [[101 Reflections]]---
    • $= dv.pages().where(page => page.hub == "[[101 Reflections]]").length
    • This worked as expected. But I wanted to use inline metadata so I could actually link to the [[101 Reflections]] page.
  • Tried changing metadata value to work as an inline hub:: [[101 Reflections]] input. - Did not return anything; just showed the query in red.
  • Tried changing the inline query to $= dv.pages().where(page => key.hub == "[[101 Reflections]]").length - Did not return anything; just showed the query in red.
  • Tried changing the inline query to $= dv.pages().where(key => key.hub == "[[101 Reflections]]").length - returned “0” (should have returned “2”).
  • Even tried changing the inline metadata to `hub::[[101 Reflections]], thinking maybe the space was interfering with it. Didn’t work.

What I’m trying to do

Basically I just want to use inline metadata rather than YAML frontmatter. The reasoning is that I want to have an easy way of linking and navigating back to a central “hub” such as a MOC or a dashboard while also providing metadata that allows me to count the number of files that link to a given file.

I considered just doing a table that counted the number of incoming links:

  • dataview table length(rows) as Number from [[101 Reflections]] sort asc
  • and it kind of works, and probably has potential if I work out the kinks, but I realized that it would link ALL things that link to it, and it would make a table unnecessarily. (As of now, that query does not even work to do what I initially wanted, much less what I actually wanted). If absolutely needed, I could manually exclude certain files, but that would get unweildy. The beauty of the initial thing is that it synergizes with templates while still allowing for deliberate linking rather than full automation.

An update: I’ve been playing around, trying to figure it out. Still no luck, but I realized that it has something to do with the inline links. If I query for just the value “101 Reflections” under the hub key, it works as long as I don’t have the double square brackets. That led me to find this page which may help, but right now I am still trying to work on it and it’s not currently working.

Final update: Yes, that thread works.

For future reference, if you want to query using inline metadata with an internal link, you can enclose the internal link with quotes.

  • Here was my Query:
    • $= dv.pages().where(page => page.hub == "[[101 Reflections]]").length
  • Here was the inline metadata:
    • hub:: “[[101 Reflections]]”

Hi sewpungyow,

I think the issue you’re running into is that YAML syntax in the frontmatter is different than DataView syntax in the page. YAML does not support Obsidian link syntax. In the YAML front matter, the value [[101 Reflections]] does not mean “a link to the page called ‘101 Reflections’”, it means "An array that contains an array that contains the string “101 Reflections”.

So in your YAML, you have: hub: [[101 Reflections]], which YAML converts to a nested array and assigns to hub. But in your query, you have page.hub == "[[101 Reflections]]", so you’re comparing page.hub, which is an array object, to "[[101 Reflections]]", which is a string. Since an object is not the same thing as a string, the comparison fails and you get no results.

Likewise, when you changed your YAML to hub: "[[101 Reflections]]" (with the quotes), you set the value of hub to a string, so when you compare it in your query you’re comparing two strings and it succeeds. If that’s all you need, then your solution is fine.

However, if you want to treat the hub field as a link, I think you need to use a real Dataview field in the body of the doc, e.g. hub:: [[101 Reflections]]. I don’t think links in YAML are supported – but I welcome correction on that! :slight_smile:

I hope this helps!


1 Like

Hi Craig,

Thanks for the information! I have been playing with it, and my initial issue was that for some reason, hub:: [[101 Reflection]] is not compatible either. From some trial and error and some more searching, I found that if I wrap the inline metadata with quotation marks: hub:: "[[101 Reflection]]", it does work.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.