Advanced queries and the database documentation?

Hi

I have already found the following information:

This is useful information but I am still not able to write the queries I need.

What I am still missing is:

  • Information about which attributes belong to the same element (i.e. share the same element ID).
  • The information which values of which attributes in turn contain a reference to an element ID of another element.
  • And I still don’t understand certain parts of the syntax.

My question on the first two points is:
Is there any documentation about those aspects?

On the last point, let me give you a concrete example:

The following query basically works fine:

   {:title "⭕ open TODOs in the journal ..."
        :query [:find (pull ?h [*])
                           :where
                           [?h :block/marker ?marker]
                           [(contains? #{"TODO" "WAITING"} ?marker)]
                           [?h :block/page ?p]
                           [?p :block/journal? true]]
        :result-transform (fn [result] (sort-by (fn [h]
                                       (get h :block/priority "Z")) result)))
        :collapsed? false}

I thought I understood how this query works and tried to enhance it.
I want to exclude all the TODO and WAITING blocks that contain the keyword “subtask”.

So if I have the structure …

  • TODO main task one
  • TODO main task two
    • TODO subtask 2.1
    • TODO subtask 2.2

… I want the query to return only the two “main tasks” and NOT the subtasks (2.1 and 2.2.).

So I added this part to the query:

               [?h :block/content ?content]
               (not [(contains? #{"subtask"} ?content)])

which leads to this query…

	{:title "⭕ open TODOs in the journal ..."
    :query [:find (pull ?h [*])
                       :where
                       [?h :block/marker ?marker]
                       [(contains? #{"TODO" "WAITING"} ?marker)]
                       [?h :block/content ?content]
                       (not [(contains? #{"subtask"} ?content)])
                       [?h :block/page ?p]
                       [?p :block/journal? true]]
    :result-transform (fn [result] (sort-by (fn [h]
                                   (get h :block/priority "Z")) result)))
    :collapsed? false}

But this query is not working. I assume there are some very basic things I still don’t understand.

SUMMARY:

  • Is there any documentation about which attributes belong together to which elements?
  • Is there documentation on which attributes contain element IDs as values?
  • Can someone help me with the query above?

Thanks
Chris

13 Likes

I’m having hard time too to understand that language. It seems i’m missing a big part of Logseq without being able to use query. :frowning:

5 Likes

Logseq simple queries are intended for those not comfortable in datascript/datalog/datomic language. I collect examples of powerful simple queries for future usage:

  • Find all blocks referencing the page: [[Logseq]], #Logseq or tags:: Logseq.
    {{query [[Logseq]]}}
  • Find all pages with page tags tags:: book .
    {{query (page-tags book)}}
  • Find all pages with both page tags logseq & queries.
    {{query (and (page-tags logseq) (page-tags queries))}}
  • Find all pages with page tags logseq or queries
    {{query (page-tags logseq queries)}}
  • Find all Todo blocks doing or now
    {{query (todo doing now)}}
  • Find Todo blocks done in last 7 days.
    {{query (and (todo done) (between -7d today))}}
  • Find all Todo/Doing Now/Later blocks in current page.
    {{query (and (todo todo doing now later) (page <% current page %>))}}
  • Find all Todo/Doing Now/Later blocks where tiensonqin was tagged.
    {{query (and (todo todo doing now later) [[tiensonqin]]) }}
11 Likes

Thank you for your participation in the discussion.

For me, the problem is not that I don’t feel comfortable learning the advanced queries, but that important information is missing and prevents this.

On the one hand, simple queries don’t work as they should in theory and are still very limited:

On the other hand, important information is missing to write the advanced queries myself (that’s why I asked for more documention).

Fortunately, there are very helpful people here who also help with concrete requests :heart:, but some request stay unanswered.

In the meantime, I’m also collecting all kinds of practical queries and trying to learn from them.

7 Likes

Hello,did you solve the problem? I have the same problem as yours.(I have task and sub tasks,and I want show them clearly)

3 Likes

Hi Chris,

Have you had any success with queries that exclude attributes? I just started using Logseq and learning about DataScript and having the same issue. While structuring my Logseq graph, I’m doing something similar with tagging my work tasks with #work/todo, and now I need to exclude those pages/tags when looking at my personal tasks.

I can use a simple query {{query (and (task TODO DOING) (not [[work/todo]]) ) }}, but I like the advanced query features like adding a title and initially collapsing.

I found this link that hints at excluding using get-else, but I wasn’t successful using it within Logseq (not sure if this would be available within Logseq’s advanced query). datascript-tutorial/datascript.md at master · kristianmandrup/datascript-tutorial · GitHub

While it doesn’t directly answer your question (and is more of a workaround), if you tag your main tasks with #MainTask, then you could target that page name in your query. I’m using the below query on my work tasks page - if I can’t find a solution for my personal tasks, I might just start tagging those with something like #NicksTasks.

#+BEGIN_QUERY
{:title "Work priority A items" :query 
[:find (pull ?b [*])
 :where
 (task ?b #{"NOW" "LATER" "TODO" "DOING"})
 [?b :block/priority "A"]
 [?b :block/refs ?p]
 [?p :block/name "work/todo"]
]
 :collapsed? true}
#+END_QUERY

Nick

Hello Nick,
I’m sorry. I have collected the necessary queries and have been getting by with them for a while now.
I haven’t delved any deeper into the matter.