Reputation: 7736
Given a collection like this:
collection : [
'doc1.json' : {
someXPath : [
{ expression : 'a' },
{ expression : 'b' },
{ expression : 'c' },
],
otherLargeObject : [ ... ]
},
'doc2.json' : {...},
'doc3.json' : {...}
]
Using Xquery I can select child nodes of the collection as follows:
fn:doc()//someXPath/expression/text()
resulting in something this:
[ 'a', 'b', 'c', ... ]
The above syntax is concise and the result only includes the data I want.
Using Javascript I would try the following:
var results = [];
for (var doc of fn:doc()) {
if (doc.someXPath) {
results.push(doc.someXPath.map(function (x) {
return x.expression;
})
}
}
The javascript code is verbose and not as versatile. The xquery example matches other document structures with different levels of nesting without the need for extra conditions.
Does the doc iterator retrieve the entire document in memory or just the parts that are accessed? e.g. is otherLargeObject loaded into memory in the javascript version? Is the javascript version as efficient as the xquery version?
Is something like the following possible?
var result = [];
for (var justTheNodesIWant in fn:doc()('//someXPath/expression/text()') {
result.concat(justTheNodesIWant);
}
result; // ['a', 'b', 'c', ... ]
Is there an equivalent way to achieve the xquery result and performace using server side javascript?
Upvotes: 1
Views: 221
Reputation: 8422
The below will work:
var results = [];
for (var exp of xdmp.xqueryEval('fn:doc()/someXPath/expression')) {
results.push(exp);
}
results
The downside of that is the mixing of languages, including embedding code in a string.
Upvotes: 3