在Marklogic Javascript中实现For Loop? [英] Implement For Loop in Marklogic Javascript?
问题描述
我可以使用下面的XQuery获取集合中所有文档的URI:
I can get all the URIs of the documents in a collection by using below XQuery:
for $doc in fn:collection("transform")
return xdmp:node-uri($doc)
但是,当我尝试在MarkLogic的Javascript模块中实现此功能时,它仅获取数据库集合中的最后一个文档.
But, when I tried to implement this in a Javascript module in MarkLogic, it is getting only last document in the database collection.
'use strict';
declareUpdate()
var docs = fn.collection("transform");
for(var doc of docs) {
xdmp.nodeUri(doc)
}
它没有提供集合中的所有URI,而是仅返回文档的最后一个URI.
It is not giving all the URIs in the collection, but rather it's only returning the last URI of the document.
如何获取它以返回所有URI?
How can I get it to return all of the URIs?
推荐答案
创建一个数组并将每个URI添加到for循环中的该数组中,然后返回该数组:
Create an array and add each of the URIs to that array in your for loop, then either return the array:
'use strict';
declareUpdate()
var docs = fn.collection("transform");
var results = [];
for (var doc of docs) {
results.push(xdmp.nodeUri(doc));
}
results;
或使用 Sequence.from()
:
'use strict';
declareUpdate()
var docs = fn.collection("transform");
var results = [];
for (var doc of docs) {
results.push(xdmp.nodeUri(doc));
}
Sequence.from(results);
但是,如果您只想返回URI,则使用 cts.uris()
和 cts.collectionQuery()
:
However, if you simply want to return the URIs, then it would be better/easier to use cts.uris()
with a cts.collectionQuery()
:
'use strict';
declareUpdate();
cts.uris("", null, cts.collectionQuery("transform"));
这篇关于在Marklogic Javascript中实现For Loop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!