None of the answers explain why they address the origin question, or what the core misunderstanding is. They are correct answers but its not obvious why.
There is no problem with the original code except the incorrect assumption of how JavaScript returns evaluated expressions.
The LAST evaluated expression is returned from a JavaScript 'script'.
This differs from XQuery in which the expressions accumulate into a Sequence which is returned.
Try this simple case:
'use strict;'
var i = 0
i++
i++
i++
Result:
2
NOT
[ 0 , 1 , 2 ]
That doesnt mean it didnt 'execute' "i++" 3 times, it means the resulting value is the last one.
Try your original code unchanged except in the loop 'Do Something' that has some kind of visible result -- say Console.log()
'use strict';
declareUpdate()
var docs = fn.collection("transform");
for(var doc of docs) {
Console.log(xdmp.nodeUri(doc))
}
Then look in ErrorLog.txt (V8) of 8000_ErrorLog.txt (V9)
You will see an entry for each URI.
That is WHY the suggestion to put the values into an array, since you asked "How can I get it to return all of the URIs?" - thats how you do it.
However the question "Is there any way to read directly the uri's rather than pushing into array" is entirely different. Your existing code currently does that. You just don't 'see' it because you are doing nothing with the URI but 'dropping it on the floor'
When you add the code to do whatever it is you want to do with the URI (or node), it will be executed one by one without having to load the entire sequence first.
Try it.