Help Me to correct my Dataview Query

Problem

I have the following files in obsidian.

/chest.md
/post.md
/index.md
/pest/pest.md
/test/rest.md
/test/test.md
/arch.md

Among Them chest.md and test/test.md have the below content.

link:: [[index]]

And test/rest.md and arch.md have the below content.

pink:: [[index]]

I want a dataview query that results files with pink:: [[index]] and files in root directory (not subdirectories) with no link:: [[index]]. This Dataview Query will be placed in index.md

So the result should be.

test/rest.md
arch.md
post.md

My Failed Solution

TABLE
WHERE pink = this.file.link OR file.folder AND link != this.file.link
SORT file.name asc
SORT page-no asc

Got Result:

test/rest.md
arch.md
pest/pest.md

Support Please

You didn’t mention it, but I reckon you don’t want the index to be included in your list neither, so I kept that out of the loop, as well.

First of all, there are some issues with your query:

  • You don’t group your OR and AND, leaving it slightly undecidable which triggers when. I’m not sure which has precedence, but I tend to add group them just to be on the safe side
  • You’re using link as one of your fields. There is already a file.link present in the metadata, which might cause some issues now and then. So I would consider renaming that one

Here is my attempt at this query:

```dataview
TABLE file.folder, pink, link 
WHERE file.path != this.file.path AND
  ( !file.folder AND ( !link OR link.path != this.path)) 
  OR
  ( pink AND pink.path = this.path )
```

It basically has three criteria for a file to show:

  1. Not the current file, aka `index``
  2. In root folder, aka !file.folder, and either of the following:
  • No link field
  • If link field, verify that it doesn’t point to my self
  1. Not in root, aka file.folder, and has a pink field which points to my self

Hopefully, that is what you’re looking for, and that you understand how to group your queries to avoid any side effects due to precedence of the various boolean operators.

1 Like

Thanks, A Great Help, It worked. But, If I wan’t the same functionality in test/test.md So then the result must be only rest.md.

But the code snippet you gave does not work as that in test/test.md.

Note as there it was index.md, here it is test.md but not on root directory, instead in a test directory.

I am looking for a common dataview query snippet that works the same way on any directory for it’s index file.

However, this code snippet in test.md result gave me:

arch.md
post.md
rest.md
index.md

I’m not quite following you here. Maybe try restate what you’re wanting, and what are the symbolic meaning of link and pink. Are they link parents, siblings, main topic, or what?

They are fields linking to the index file. For Root folder index file was index.md and for test folder it is test.md. What I want is to list all files in that folder (not from it’s subfolders) that is not linked at all to that folders index file or the files that is linked to the index file using the field pink not by link and that is not limited to the base folder it can search for other folders as well.

So when it comes to test folder it has two files test.md and rest.md.

As rest.md is not linked in anyway it must be in the dataview query results. And there is no any other files in the vault that links to the index file using pink field. So that the only result for this query will be rest.md for now.

%%How Field will be mentioned if required%%

pink:: [[index file]]

So to resummarise the summary:

  • The script will be used in potentially many folders, and you’ll insert it into the “index” file of that folder
  • In such a folder, link will point to that index file, and you don’t want them to show up in the query
  • Outside of such a folder, a pink reference might link to the current folders index, and in that case it should show

This bargains for a slightly different solution, that when it’s just in one folder. So my new suggestion is to use a dv.view() script, which you’ll reference the same way in any of your index notes (in whatever folder you’ll place it in). This way, if you need to change something, you change it one place, and all the various “index” notes gets the update at the same time.

The dv.view script I used when testing

f54318LinkAndPink.js.zip (422 Bytes)

Uncompress the file above into a dedicated folder for your dv.view() scripts. In my test vault, that is the _js folder. Feel free to change it to whatever you like, and be sure to change the references below to match that folder.

You’ll most likely need to adapt it a little to your case, like possibly remove the FROM #f54318 and change what is the output in the table. If you want a list output, more changes needs to be done, but please come back to me if that is the case.

The main point in that script is either way the WHERE clause, which is like this:

WHERE 
  file.path != this.file.path AND
  (
    ( file.folder = this.file.folder AND !link)  
      OR
    ( file.folder != this.file.folder AND pink AND pink.file.path = this.file.path )
  )

The explanation is as follows:

  • First of all exclude the current index file from the list
  • Now match either of the following conditions:
    • If we’re looking at files in the same folder, file.folder = this.file.folder, then only pull out files which don’t have the link attribute, !link
    • If we’re looking at files in another folder, file.folder != this.file.folder, then check if that file has a pink field, and if it does, check whether that refers back to this index file

In my test setup, I’ve included the following in both index and test:

```dataviewjs
await dv.view("_js/f53418LinkAndPink")
```

Output from index:

Output from test:
image

PS: If you can’t get the syntax correct for your query within the dv.view() script, please post the query which has the output you like, and I’ll try to give you a better version.

PPS: Note that the view script file needs to end in .js, and it needs to be a clean text file for it to work. I’ve added the plaintext plugin, and configured in so that I can also see .js files in my vault. Another way to do this, is to use a text editor (like VS Code) outside of Obsidian.

1 Like

Thanks, I am not using javascript, however the where statement you gave me is working for my needs.

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