obsidian-dataview icon indicating copy to clipboard operation
obsidian-dataview copied to clipboard

Is it possible to check if a field exists?

Open wenlzhang opened this issue 4 years ago • 11 comments

I tried with date-started = null, and it seems that this treats files with Date Started:: and files without this field both as null. Is it possible to distinguish these two cases?

wenlzhang avatar Jun 17 '21 23:06 wenlzhang

Good point, I'm not sure you can differentiate in normal Dataview; will add support for this.

blacksmithgu avatar Jun 18 '21 20:06 blacksmithgu

Is it possible to achieve this with dataviewjs then? I would like to try it for now, and would really appreciate it if you can illustrate this with an example. 😃

wenlzhang avatar Jun 18 '21 21:06 wenlzhang

It's possible in dataviewjs - dv.pages(<query).where(p => "date-started" in p).

blacksmithgu avatar Jun 18 '21 21:06 blacksmithgu

Thanks for the solution. I have a follow-up question regarding this. After some testing, it seems that

  • Query 1: dv.pages(<query).where(p => "date-started" in p) gives files that have the field Date Created and with values in it.
  • Query 2: I was expecting that dv.pages(<query).where(p => !("date-started" in p)) would give files that do not have the field Date Created included. Instead, it also gives files have the field Date Created but with no values in it.

I was trying to use some kind of regular expression to separate the two cases in Query 2 but with no luck. Is there a workaround to separate the following files?

  • Files that have Date Created and with values, i.e. date like 2021-06-19
  • Files that have Date Created but with not values
  • Files that do not have Date Created

wenlzhang avatar Jun 19 '21 13:06 wenlzhang

That seems wierd - pages with something like Date Created:: should have a date-created field that is set to null, whereas pages without it should not have the key at all.

blacksmithgu avatar Jul 30 '21 06:07 blacksmithgu

I believe contains() should work for this. Can you please try it out and let us know?

AB1908 avatar Oct 29 '22 21:10 AB1908

I believe contains() should work for this. Can you please try it out and let us know?

What would be the syntax for using contains()? Is it possible to distinguish the following?

- Files that have Date Created and with values, i.e. date like 2021-06-19
- Files that have Date Created but with no values
- Files that do not have Date Created

wenlzhang avatar Oct 30 '22 10:10 wenlzhang

Not entirely sure since it would depend on the indexing but you can do contains(file, "section") to check attributes on a specific object.

AB1908 avatar Oct 30 '22 15:10 AB1908

It seems like we're missing something to refer to the file being queried as an object in itself. Doing = contains(this, "date-started") works as intended, but doing something like contains(row, "date-started") doesn't return anything (since row seems to get its value when actually referencing an index.

So do anybody now what would be the equivalent of this for the current file being queried? The page/row object, so to speak.

holroy avatar Feb 03 '24 16:02 holroy

this should be the current file object AFAIK. I'm out of touch with DV's source so no idea what row refers to. What is the use case at hand?

AB1908 avatar Feb 03 '24 23:02 AB1908

The use case is to differ between the defined but null, or not defined in a query. A silly query to showcase this scenario:

```dataview
TABLE contains(this, "date-created"), contains(row, "date-created"), row[date-created]
LIMIT 5
```

Here the first column would always show whether date-created is defined in the current file, but the second case to show whether date-created is defined in the first five file of your vault doesn't work. And I'm looking for what kind of expression would work for that case. (And row can be used to do row[date-created], and is used to allow for having fields like where or sort or similar, which otherwise would be interpreted as part of the query language)

The above query in my test vault displays as: image

And ideally I would like for the second column in the third and fifth row to actually show "true". In the fifth row, it's defined but don't have any value, which is the case we're trying to discern in this thread.

holroy avatar Feb 04 '24 14:02 holroy