How to get the first element, second etc of a tuple?

There’s an assumption that the :find outputs a tuple. But this is not what happens.
I cannot speak to why this doesn’t happen, but that has probably to do with the implementation Logseq uses.
So instead you just get a list of values for your result.
Consider my test:

#+BEGIN_QUERY
{:title "Blocks"
 :query [
        :find ?b ?name
        :where
        [?b :block/name ?name]
        [(>= ?b 2395)]
 ]
}
#+END_QUERY

Notice that there are 6 results! Not 3.

We can use pull to get a “better” result…

#+BEGIN_QUERY
{:title "Blocks"
 :query [
        :find (pull ?b [:db/id :block/name])
        :where
        [?b :block/name ?name]
        [(= ?b 2395)]
 ]
 :view (fn [info] [:div (for [arg info] (second arg))])
}
#+END_QUERY

(don’t ask why it is suddenly the second argument… idk…)

To see what happened, we can use:

#+BEGIN_QUERY
{:title "Blocks"
 :query [
        :find (pull ?b [:db/id :block/name])
        :where
        [?b :block/name ?name]
        [(>= ?b 2395)]
 ]
 :view (fn [r] [:pre.code (pprint r)])
}
#+END_QUERY


Now there are actually 3 results. Same can be achieved with :keys as mentioned by @mentaloid .

That just leaves me with the question of… why?
Why would you want to retrieve this information in the first place? It shouldn’t be something users are otherwise concerned with.

1 Like