Determine if an element from a list is present in another list with Dataview

To link several notes together, I’m using a custom custom metadata ‘references’. For instance:

---
aliases:
- 'Note1'
references:
- 'Note 2'
--- 
# Note 1
---
aliases:
- 'Note2'
references:
- 'Note 1'
--- 
# Note 2

In each notes in the foot section, I use this code:


    ## Other Notes

    ```dataview
       LIST WHERE contains(this.references, file.name)
    ```

    ## Referring Notes

    ```dataview
        LIST WHERE contains(references, this.file.name)
    ```

How can I do the same with aliases and note only the file name ?

I tried something like this, but obviously it does not work. (the file.aliases field does not contained the this.references list but only its elements).

   LIST WHERE contains(this.references, file.name) OR contains(this.references, file.aliases)

My first question:

  • In “references” field, why you don’t use the link for your reference file? For example:
---
aliases:
  - 'Note1'
references:
  - "[[Note 2]]"
---

(Links in yaml frontmatter don’t account as backlinks - inlinks or outlinks - neither in graph. So, you can use them without problems)

I could use this. But I’m not sure to see the point.

Because in same cases it’s more accurate to work with links than with strings.
But before let’s put things in this way.

  1. Why use yaml format with syntax for “lists” and not for a single elements? In same cases you have “multiple” values for same field?
    If only one value, it’s more advised to use this format:
---
aliases: Note1
references: Note 2
--- 

Why? Because the function contains() works in different ways if a list or a string.

For example, your first query is inverted and not work well. The function contains works with this logic: contains(object-or-list-or-string, value).
Your query ‘logic’ should be: “where” the “file.name” contains the value in “this.reference”:

```dataview
LIST
WHERE contains(file.name, this.references)
```

If the value “this.references” is one element (in the format “references: Note 2”) then query presents all files with “Note 2” in the name (if any with the name “This is my Note 2”, it’s listed too).
But if the value “this.references” is a “list” (with one or more values), then the query results are a mess.

  1. If you use links instead of “strings”, you reduce some issues, because you don’t need to find a relation between your “string” (or “strings”) and file names/links: you have links and this is enough. If links in your field, you just need to query your references (and if your notes have aliases it’s possible to replace the file name by file aliases):
---
aliases: Note1
references:
  - "[[Note 2]]"
  - "[[Note 3]]"
--- 
## inline query

`=this.references`

## list

```dataview
LIST WITHOUT ID references
WHERE file.link = this.file.link
```

## list (display links by aliases)

```dataview
LIST WITHOUT ID link(references, references.aliases)
WHERE file.link = this.file.link
```

  1. If instead of yaml frontmatter fields you use inline fields, then the links count as backlinks and you can use queries to display file.inlinks and/or file.outlinks.
ref:: "[[Note 2]]", "[[Note 3]]"
## inline query

`=this.ref`

`=this.file.outlinks`

## list 1

```dataview
LIST
FROM outgoing([[#]])
```

## list 2

```dataview
LIST WITHOUT ID file.outlinks
WHERE file.link = this.file.link
```

## list 3 (display links by aliases)

```dataview
LIST WITHOUT ID link(file.outlinks, file.outlinks.aliases)
WHERE file.link = this.file.link
```

1 Like

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