Query from lists using dataview

What I’m trying to do

Hey everybody,
I am trying to define a key with a list of values in yaml front matter (or in-line, see below), and then use dataview to make a list (or table or whatever) of all files that contain one specific value in that list. e.g.:

the front matter bit:

---
type: book
copy:
- library1
- library2
- personal
---

and the dataview query:

```dataview
LIST
FROM "books"
WHERE copy = "library2"
```

somehow, dataview and/or yaml don’t seem to accept the lists I define. If I use only one value, it works fine. as you can probably guess, I organize my literature with obsidian and I want to note in which libraries I can find a particular book and then generate lists for each library so I can see what to get on my library visits.

what I’d like even better would be using standard obsidian tags instead of front matter, cause the former offer auto-complete, so I don’t have to remember all the shorthands for the libraries. Is there a any way to achieve this?

Things I tried

I also tried to define the lists with square bracket, without brackets just separated by commas and inline key-value pairs (key:: library1 library2 etc).

It would be so cool if anyone could point me in the right direction to make this work. Thanks!

By parts…

  1. Using frontmatter fields
  • To arrays, you need to add two spaces before “-”
---
type: book
copy:
  - library1
  - library2
  - personal
---
  1. Dataview query
  • If you want to filter one of the array elements, you need to use the function contains(field, "value") (you can use copy = "library2" if you have only one value, not a list/array).
  • Try this query:
```dataview
LIST
FROM "books"
WHERE contains(copy, "library2")
```
  1. Using tags
  • If, instead of frontmatter values, you prefer using tags (#library1, #library2, …) anywhere in your file, you can use for the query the implicit field file.tags… or, more simple, define tag as the source (FROM #tag).
  • Try this query
```dataview
LIST
FROM #library2
```

EDIT: You can use multiple source filters. So, if you want keep the filter to folder “books” just compose the source filter with the expression FROM "books" AND #library2

6 Likes

Hello.

The YAML sample in Obsidian Help doesn’t use spaces in arrays, and the OP’s YAML works in the sample note attached without using spaces.

But is it better to use spaces in arrays?

Thanks

Angel

SampleNote.md (407 Bytes)

Hello.

For Dataview plugin in particular, following the advice from some ‘code experts’, it’s more advisable to use the two spaces (because “YAML is an indentation-based markup language”).

The same for ‘nested’ keys/fields Dataview Documentation:

field1:
  field2: value1
  field3:
    - value2
    - value3
1 Like

Brilliant.

Thank you very much for the explanation and link.

I learn. Slowly.

Angel

The official YAML specs say:

In YAML structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line.

So if indentation can start at zero spaces, I guess that’s why the example in Obsidian Help uses zero spaces; although the dataview examples differ.

At least both methods appear to work with dataview.

Consistency of indentation in a document seems to be critical.

Thanks again for your help and time. No need to reply. Just sharing what I read for anyone else who might read this thread.

Angel

EDIT

Curiously (to me, anyway) the Obsidian Help example uses hyphens, but the dataview documentation doesn’t.

My head :face_with_head_bandage:

Thanks mnvwvnm! The last one is so simple, I should have thought of it myself :slight_smile:

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