Dataview "contains" help

I had book pages with the frontmatter:

---
Author: name
Alias: Booktitle
Year: 2020
Medium: Book
Length: 10
Rating: 8
DateStarted: 2023-01-09 
DateFinished: 2023-02-03
Cover: weblink
Tags:  tags
Country: 2-letter code
---

I changed this, in order to link the book page (with the above YAML), to an author note, with the following YAML:

---
Alias: Stephen King
Full Name: Stephen Edwin King
Years: 1947-
Photo: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg
Tags: Horror
Country: US
cssClasses: cards, cards-cover, cards-2-3, table-max, max
---

I changed a simple string after Author, e.g. Author: Stephen King to "[[King, Stephen|Stephen King]]" (using the plugin GitHub - Trikzon/obsidian-frontmatter-links: An Obsidian plugin that renders links in a note's frontmatter as links.) so that a new book page looks like this:

---
Author: "[[King, Stephen|Stephen King]]"
Alias: It
Year: 1986
Medium: Book
Length: 1166
Rating: 10
DateStarted: 2021-06-02
DateFinished: 2021-06-30
Cover: https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1327118909l/11381895.jpg
Tags: Horror
Country: US
---

This renders the author’s name as just the name, so outwardly nothing has changed, and the author is thus linked.

HOWEVER: Now my dataview table that contains the authors name to compare the author name to the book’s author (so I can list the author’s books on the author’s page) will no longer work. It appears the quotation marks around the link in the frontmatter (necessary for the link) breaks the dataview table.

This is the author page table:

```dataview
Table
Title, Year
FROM "000"
WHERE contains(Author, this.Alias)

Is there any way I can retain the Author link while still being able to query dataview to list the books? Contain doesn’t seem to work with this…

If I get this to work, I can then also do the same for the Country code, so Country: could be followed by "[[United States|US]]" rendered as just US, but it would then be linked to a country page.

Any advice appreciated :slight_smile:

1 Like

The following works locally but not clear if it is really doing what you want or is just a fudge.

---
Alias: [[Stephen King]]
Full Name: Stephen Edwin King
Years: 1947-
Photo: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg
Tags: Horror
Country: US
cssClasses: cards, cards-cover, cards-2-3, table-max, max
---
# dataview
- This file in folder called `_inbox`.
- File called `It` in folder called `000`.
- Only change is to the alias above from `Stephen King` to `[[Stephen King]]`
```dataview
Table
Title, Year
FROM "000"
WHERE contains(Author, this.Alias)
```

That’s good, but unfortunately without the quotation marks in the frontmatter the link doesn’t exist (which makes the square brackets unuseful). Any other ideas? Thanks :slight_smile:

1 Like

I looked at this for a couple of hours last night and again this morning for another hour. Manual coding of values (1 and 2 below) works with the original YAML, so I assume it must be possible with keys as well.

Tried meta(link) as well, thinking that the Author key in the book page is now a link, but no luck. Blundering around in the dark

Main thoughts: I expect someone like holroy or craig or mnvwvnm would know what to do here in an instant.

Secondary thought that was Datacore is going rip things up quite radically.

Views in Datacore are editable and no longer use the Dataview Query Language.

I’ll keep stumbling and post if I strike lucky.

---
Alias: Stephen King
Full Name: Stephen Edwin King
Years: 1947-
Photo: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg
Tags: Horror
Country: US
---

# original
```dataview
Table
Title, Year
FROM "000"
WHERE contains(Author, this.Alias)
```
# 1
```dataview
Table
Title, Year
FROM "000"
WHERE Author = [[King, Stephen|Stephen King]]
```
# 2
```dataview
Table
Title, Year
FROM "000"
WHERE contains(Author, [[King, Stephen|Stephen King]])
```

Author is defined as:
Author: "[[King, Stephen|Stephen King]]"

:white_check_mark: This works:
WHERE Author = [[King, Stephen]]

:white_check_mark: And this works:
WHERE contains(Author, [[King, Stephen|Stephen King]])

:white_check_mark: And this also works:
WHERE contains(Author, [[King, Stephen]])

:x: But this doesn’t:
WHERE Author = [[Stephen King]]

:x: Nor does this:
WHERE contains(Author, [[Stephen King]])

Wonder why Stephen King doesn’t get picked up when it is in the Author key?

I could :cry:

1 Like

Challenge accepted… :smiley:

No links are present in the frontmatter

First of all lets be clear on the semantics, neither of the following frontmatter fields are actual links:

---
Tags: f54123
NoQuotes: [[King, Stephen|Stephen King]]
Quotes: "[[King, Stephen|Stephen King]]"
Matches: Stephen King
---
questionUrl:: http://forum.obsidian.md/t//54123

actualLink:: [[King, Stephen|Stephen King]]

```dataviewjs
console.log(dv.current().file.frontmatter)
console.log(dv.current().actualLink)
```

And the output as shown in the Developer Tools reveals this:
image

With no quotes, the end result is actually an array with an array, and the inner array, has two elements separated by comma, ,, so it holds “King” and “Stephen | Stephen King” as its values.

And the second output, of the inline field, is an actual link (to a non-existing file in my case).

Show this frontmatter as a TABLE

Now I continued to add the following to my test file:

## Shown as a table
```dataview
TABLE WITHOUT ID  Matches, NoQuotes, Quotes, meta(Quotes), Matches = meta(Quotes).display
FROM #f54123
WHERE file.name = this.file.name
```

And the output is this:

And here it becomes a little hairy. The third column is showing the Quotes as a link, and that link is produced by dataview somewhere, and now we can also do the meta(Quotes) and reveal that the display is the part we want to match against.

So the last column shows us what the new match needs to be: Matches = meta(Quotes).display. In the case of having multiple authors, the syntax might vary a little depending on how you write it in the book page.


Note that all of the above assumes that all of your authors have note names like “King, Stephen”, and that you’ll always use an aliased version in the (fake) book entry (which is my test file), like [[King, Stephen|Stephen King]]. In other words, you might run into issues if you deviate from this setup.

Hopefully, this clarifies some of the issue, even though I’ve not written the actual query related to matching a book towards an author. Please let me know if there’s still something unclear about how to achieve that goal, and I’ll maybe write up some stuff later on today/this evening.

2 Likes

Topic

Summary

User Requirement Specification (URS):
1.The Author field, in each Book page, is either a fileLink such as “[[King, Stephen|Stephen King]]” or an array of fileLinks such as [ “[[King, Stephen|Stephen King]]”, “[[Toliken, J.R.R.|J.R.R. Tolkien]]]” ].
2.The Alias field, in each Author page, is a string such as “Stephen King”.
3.The DQL10 is in an Author page such as [[dic_19620411]], [[Stephen King]], or [[King, Stephen]].
4.The fields mentioned above, such as the Author and Alias, are correct at all times.

  • How to compare the Author field, whch is a fileLink such as “[[King, Stephen|Stephen King]]” with the Alias field, whch is a string such as “Stephen King”?

Test

Summary
  • dataview: v0.5.55

Input

Summary

dictionary files: Books

Summary_B
  • Location: “100_Project/02_dataview/Q25_MetaFunction/Q25_test_data/Books”

folder: Some

  • filename : Some Book
---
Author: "[[King, Stephen|Stephen King]]"
Alias: "It"
Year: 1986
Medium: "Book"
Length: 1166
Rating: 10
DateStarted: 2021-06-02
DateFinished: 2021-06-30
Cover: "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1327118909l/11381895.jpg"
Tags: "Horror"
Country: "US"
---



folder: Another_excluded

  • filename : Another Book
---
Author: "[[K , Stephen|Stephen K]]"
Alias: "It"
Year: 1986
Medium: "Book"
Length: 1166
Rating: 10
DateStarted: 2021-06-02
DateFinished: 2021-06-30
Cover: "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1327118909l/11381895.jpg"
Tags: "Horror"
Country: "US"
---


folder: One_excluded

  • filename : One Book
---
Author: "[[King Test, Stephen|Stephen King Test]]"
Alias: "It"
Year: 1986
Medium: "Book"
Length: 1166
Rating: 10
DateStarted: 2021-06-02
DateFinished: 2021-06-30
Cover: "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1327118909l/11381895.jpg"
Tags: "Horror"
Country: "US"
---


dictionary files: Authors

Summary_A
  • Location: “100_Project/02_dataview/Q25_MetaFunction/Q25_test_data/Authors”

folder: 04

[[dic_19620411]] = [[Stephen King]] = [[King, Stephen]]

  • filename : dic_19620411
---
Date: 1962-04-11

# the same as \[\[King, Stephen\]\]
Alias: "Stephen King"
Full Name: "Stephen Edwin King"
Years: "1947-"
Photo: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg"
Tags: "Horror"
Country: "US"
cssClasses: cards, cards-cover, cards-2-3, table-max, max
---

### DQL10



  • filename : Stephen King
---
Date: 1962-04-01

# the same as \[\[King, Stephen\]\]
Alias: "Stephen King"
Full Name: "Stephen Edwin King"
Years: "1947-"
Photo: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg"
Tags: "Horror"
Country: "US"
cssClasses: cards, cards-cover, cards-2-3, table-max, max
---

### DQL10



  • filename : King, Stephen
---
Date: 1962-04-01

Alias: "Stephen King"
Full Name: "Stephen Edwin King"
Years: "1947-"
Photo: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg"
Tags: "Horror"
Country: "US"
cssClasses: cards, cards-cover, cards-2-3, table-max, max
---

### DQL10



folder: 12_excluded

  • filename : Stephen K
---
Date: 1962-12-01

Alias: "Stephen K"
Full Name: "Stephen Edwin K"
Years: "1947-"
Photo: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg"
Tags: "Horror"
Country: "US"
cssClasses: cards, cards-cover, cards-2-3, table-max, max
---


  • filename : Stephen King Test
---
Date: 1962-12-07

Alias: "Stephen King Test"
Full Name: "Stephen Edwin King Test"
Years: "1947-"
Photo: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Stephen_King%2C_Comicon.jpg/330px-Stephen_King%2C_Comicon.jpg"
Tags: "Horror"
Country: "US"
cssClasses: cards, cards-cover, cards-2-3, table-max, max
---


DQL10_compare_a_fileLink_with_a_string_and_TABLE

Summary

Main DQL

Code Name Data type Group By Purposes Remark
DQL10
_compare_a_fileLink
_with_a_string
_and_TABLE
1.Author: (in Book pages)
1.1 a fileLink such as “[[King, Stephen|Stephen King]]”
1.2 an array of fileLinks such as [ “[[King, Stephen|Stephen King]]”, “[[Toliken, J.R.R.|J.R.R. Tolkien]]]” ]

2.Alias: (in Author pages)
a string such as “Stephen King”

3.file.name: an arbitrary string (for Author pages)
1.“King, Stephen”
2.“Stephen King”
3.“dic_19620411”
no 1.To filter by Author
2.To break up a list Author into each element OneAuthor
3.To filter by meta(OneAuthor).display
4.To display the result as a table
1.The fields such as the Author and Alias are correct at all times.

2.The DQL10 is in an Author page such as [[dic_19620411]], [[Stephen King]], or [[King, Stephen]].

Code DQL10_compare_a_fileLink_with_a_string_and_TABLE

Summary_code
title: DQL10_compare_a_fileLink_with_a_string_and_TABLE =>1.To filter by `Author` 2.To break up a list `Author` into each element `OneAuthor` 3.To filter by `meta(OneAuthor).display` 4.To display the result as a table
collapse: close
icon: 
color: 
```dataview
TABLE WITHOUT ID
      file.link AS "Book",
      this.file.link AS "current Author link", 
      meta(OneAuthor).display AS "display",
      this.Alias AS "current Author Alias",
      meta(OneAuthor) AS "meta(OneAuthor)"

FROM "100_Project/02_dataview/Q25_MetaFunction/Q25_test_data/Books"

WHERE Author
FLATTEN Author AS OneAuthor

WHERE meta(OneAuthor).display = this.Alias

```

Screenshots(DQL10):

in editing mode

when the page [[King, Stephen]] exists

The value of the meta(OneAuthor).path is “100_Project/02_dataview/Q25_MetaFunction/Q25_test_data/Authors/04/King, Stephen.md”.

when the page [[King, Stephen]] doesn’t exist

The value of the meta(OneAuthor).path is “King, Stephen”.


Reference

Summary

To get the display of a link: the P_display in the DQL10


2 Likes

So the link only is created through the plugin (GitHub - Trikzon/obsidian-frontmatter-links: An Obsidian plugin that renders links in a note's frontmatter as links.) I suppose.

I did go through and change ALL author names to the aliased versions like you say: [[King, Stephen|Stephen King]]. (which I did before testing it lmao, otherwise I might have chosen another path)

However, beyond that I don’t understand how you get the output of the Quotes thing, as I get an error if I use your short example Query. Tomorrow or whenever you get a chance would be fine, thanks, this is not a rush!

Two questions before I’m able to tackle that query, what is the actual name of your Author notes, is it “King, Stephen” (or “Stephen King”)? And how do you list multiple authors now in a Book note?

You are amazing! You both are!! Just changing the WHERE to

WHERE meta(Author).display = this.Alias

was enough to make it work.

WHERE contains(meta(Author).display, this.Alias)

also seems to work. However this doesn’t work for books with multiple authors. (see comment below)

1 Like

So, the author notes are “King, Stephen”.

And books with multiple authors seem to be an issue with the above solution. The creator of the frontmatter links plugin (Multiple links in one line? · Issue #22 · Trikzon/obsidian-frontmatter-links · GitHub) told me to do multiple authors like this:

Author: ["[[King, Stephen|Stephen King]]", "[[Toliken, J.R.R.|J.R.R. Tolkien]]"]

or

Author: 
- "[[King, Stephen|Stephen King]]"
- "[[Toliken, J.R.R.|J.R.R. Tolkien]]"

but this doesn’t seem to work with the solution from @justdoitcc as cool as it is.

Frippin joy.

WHERE meta(Author).display = this.Alias

I got sooooooooo close to that several times but kept on missing the sweet spot. Brilliant answers.

2 Likes

Indeed!! I now just need to find a way for this to work if there are multiple authors… But thanks all around!!

1 Like

But Dataview can interpret a value as a link, yes? :exploding_head:

Links in YAML Frontmatter
If you reference a link in frontmatter, you need to quote it, as so: key: "[[Link]]". This is default Obsidian-supported behaviour. Unquoted links lead to invalid YAML frontmatter that cannot be parsed.

parent: "[[parentPage]]"

Please be aware that this is only a link for dataview, but not for Obsidian anymore - that means it won’t show up in the outgoing links, won’t be displayed on graph view and won’t be updated on i.e. a rename.

I think that’s what the plugin I linked does: it links links formatted like that in the usual way. I’ not sure though.

1 Like

This is confusing, but what you say here is a key point. Dataview can interpret it as links.

Also the last paragraph of your quote is crucial: when renaming notes these fields are not updated, nor show up in graph view or as backlinks.

It seems like that this Frontmatter links plugin tries to address these issues. I’ve not used it and can’t vouch for its correctness.

All my other comments are done without considering any effects that plugin might infere.

1 Like

The link thing is definitely a grey and fuzzy issue. Think Datacore is going to be quite a shakeup, given what the roadmap says.

God helg :partying_face:

So I made myself some books, which are enclosed in the next collapsed section.

My library of books

I made five books, with the content under the file name header:

King of Obsidian

---
Author: "[[King, Stephen|Stephen King]]"
Year: 2023
---

Hobbit King

---
Author: ["[[King, Stephen|Stephen King]]","[[Tolkien, J.R.R|J.R.R. Tolkien]]"]
Year: 2024
---

Hobbit King II

---
Author:
- "[[King, Stephen|Stephen King]]"
- "[[Tolkien, J.R.R|J.R.R. Tolkien]]"
Year: 2024
---

King of the Folders

---
Author:
- "[[King, Stephen|Stephen King]]"  
Year: 2025
---

No Alias King

---
Author:
- "[[King, Stephen]]"  
Year: 2025
---

Note how the different books, uses slight variations of how to present the authors, with the final book not presenting an alias for the author.

The one and only Stephen King

King, Stephen

---
Alias: Stephen King
---
### Using FLATTEN and ECONTAINS on alias

```dataview
TABLE Author, Year, this.alias, meta(OneAuthor).path, meta(OneAuthor).display
FLATTEN Author as OneAuthor
WHERE file.folder = this.file.folder
WHERE econtains(meta(OneAuthor).display, this.alias)
```

### Using FILTER on alias

```dataview
TABLE Author, Year, this.alias
WHERE file.folder = this.file.folder
WHERE Author  AND filter(Author, (a) => meta(a).display = this.alias)
```

### Using FLATTEN  and ECONTAINS on file path

```dataview
TABLE Author, meta(OneAuthor).path
FLATTEN Author as OneAuthor
WHERE file.folder = this.file.folder
WHERE Author AND econtains(meta(OneAuthor).path, this.file.path)
```

### Using FILTER on file path

```dataview
TABLE Author, Year
WHERE file.folder = this.file.folder
WHERE Author AND filter(Author, (a) => meta(a).path = this.file.path)
```

In the previous section, I’ve included four queries, which outputs the following:

The scripts uses either a combination of FLATTEN and econtains or just a filter on the entire Author field. The two first matches against the meta( ... ).display part, aka the aliased version, and the last two matches against the actual file path to the note, aka the meta( ... ).path part.

The two last ones are the only one showing the book “No Alias King”, since that doesn’t use an aliased link as the Author. A similar case could be if you type the wrong alias, so I would recommend using the file path version, as it’s a given where the file path is, but you don’t really know which alias someone wants to use for an author. In addition, that would also allow for multiple pseudonyms (or aliases) to be used for any author.


So there you have some script alternatives, which should be adaptable to your case, and ready for use. As the files and query here are presented as something ready to run as long as you place all files in the same folder, you need to change the first WHERE clause of the scripts, to match your settings. (Or exchange it with a FROM (which needs to be before any FLATTEN lines)

1 Like

Adding

FLATTEN Author as OneAuthor
WHERE Author AND econtains(meta(OneAuthor).path, this.file.path)

works! So I marked it as the solution, just another question, none of your file.folder work, though I think they’re not necessary? But what were they meant to do?

Thanks a bunch!

They were used to make my code a self contained example. It limits the query to only use files in the current folder.

So they’re not necessary in your case, that’s correct.