Inserting properties as variables in dataview query, for dynamic querying

Hi,

I want to use variables inside dataview queries, but I have problems with properties of type list.

Here is my example:
I have two notes, with different properties:
Note 1:

---
key:
  - "[[ABC]]"
---

Note 2:

---
key: 
  - "[[ABC]]"
  - "[[XXXYYYZZZ]]"
---

now I want to have a “dynamic” dataview query in Note 3, in which I insert a variable, that I have defined in Note 3, for example:
Note 3:

variable:: [[ABC]]

This dataview works inside Note 3:

```dataview
Table key
Where contains(key, this.variable)
```

But how do I have to define the “variable”, if I want to query for notes, that have both, “[[ABC]]” AND “[[XXXYYYZZZ]]” as key-property?

Thanks in advance,
Silias

I actually thought this one would require a bit of mapping, but it turns out that contains() is fully capable of handling arrays by itself. So we only need to add some other qualifiers in addition to the function call.

So try where all(contains(key, this.variable)) and see if that doesn’t meet your needs. Below is an image from my test run:

As can be seen when doing contains() on a list, it matches against the separate values. So if then require the entire result array to all() be true, we got a complete match. As variant if you want to match against any of them, you could have used any() instead. And if you wanted to check that neither of the ones in variable existed you could do none() around the entire expression.

In the last column it also shows that this works in the case of a single link in your variable.

3 Likes

You are great @holroy :kissing_heart:

1 Like

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