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

10 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:

3 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]]) }}
10 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.

6 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