How to iterate over records in a group with JavaScript?

I try to extract information (name, tags, custom meta data) from all records in a group with JavaScript.

var group = db.records.whose({ name: "Ausgaben" }).children.whose({ name: { _contains: year+'-' }});

group should be a list (aka array) of the records. However, I can’t use forEach with group, only with group(). Apparently, that returns an array. Now

group().forEach(rec =>
should set rec to each element of this array in turn. However, rec.name() does not work (i.e. it returns an error), while rec[0][0].name() does.

Consequently group()[0][0]does give me the list (aka array) of all the records I need. So
group()[0][0].forEach(rec => ...) makes rec.name() work inside the forEach.

But WHY? And: is there a way to directly obtain the list of records from the database without these arrays-in-arrays-in-functions constructs?

Using the “search” command might be a more efficient & powerful way to retrieve a list of records matching certain conditions.

Thanks for the hint, that looks really a lot more promising. However, I’m running into some weird things (or maybe I just didn’t find the explanation in the doc?)

Firstly, "kind: group and name: bla"as search string returns an empty search result (and it does so too, if I type it in DT3)
However, "name: bla and kind: group" returns all groups named “bla” (same when typing it in DT3)
I find that counter-intuitive, in my opinion the search result should not depend on the order of the parameters (yes, I know about short-circuiting AND evaluation, but that makes no sense here).
Secondly, I want to limit the search to one database:
"name: bla and kind: group scope: DB"
That works when typed into DT3’s search field and returns only the one group “bla” in database “DB”. However, in my script the exact same string returns an array with four elements, exactly the same as if there were no scope: parameter.
This happens regardless of the scripting language, i.e. AppleScript ignores the scope: parameter and returns four elements, too.

The script command doesn’t use the scope parameter, the “in” parameter can be used to specify a group or database.

Right. That works fine. However, the issue with the order of kind/name in the query remains.

Operators like AND/OR etc. are only used by text-based queries, they can’t be used to combine other conditions like name: and kind:

Indeed. If I leave out the AND, the search works regardless of the order of the parameters (in DT3 and in the script). So apparently “and” is implied here.

On the other hand, the documentation has an example using the OR operator in the appendix “search prefixes”:

any: name:test OR imprint {any: tags:blue; red}

The OR operator just combines the words test & imprint in this case (see advanced search after entering this in the toolbar search field).

I see. May be I should read the documentation more carefully.

Anyway, back to the search method. In order to get the groups for all databases, I use

var DBs = app.databases();
DBs.forEach(db => {
  var bla = app.search("kind:group kind:!tag", {in: db});

However, DT3 seems to ignore the in parameter, as seen in the Replies part of script editor:

app = Application("DEVONthink 3")
	app.databases.byId(4)()
		--> app.databases.byId(4)
	app.search("kind:group kind:!tag", {in:app.databases.byId(4)})
		--> [app.databases.byId(2).parents.byId(10387), app.databases.byId(2).parents.byId(12), app.databases.byId(2)

It seems that although I restrict the search to DB id 4, it only returns entries from DB id 2. This is actually the first of the databases in DBs.
I tried using for instead of forEach, no change.

for some reason does not limit the search command to the current database db. However,

var bla = app.search("kind:group kind:!tag", {in: db.root()});

does. So that’s apparently the way to loop over all databases with JavaScript in DT3

A database is not a record but the in parameter requires a record.

Thanks again. It’s all in the documentation, I just have to read it more carefully.