Evaluating JavaScript Queries
You can use AdhocQuery and Session.submitRequest to evaluate either XQuery or Server-Side JavaScript queries on MarkLogic Server. By default, XCC assumes the query language is XQuery. Use RequestOptions.setQueryLanguage
to specify JavaScript instead. For example:
// Create a query that is Server-Side JavaScript AdhocQuery request = session.newAdhocQuery("cts.doc('/my/uri')"); // Set the query language to JavaScript RequestOptions options = new RequestOptions(); options.setQueryLanguage("javascript"); // Submit the query request.setOptions(options); ResultSequence rs = session.submitRequest(request);
You can the results of your query in the usual way. When a ResultItem in the result sequence is a JsonItem, you can extract the item as Jackson JsonNode
and use the Jackson library functions to traverse and access the structure.
Note that there is a difference between returning native JavaScript objects and arrays returning JSON nodes from the database:
A JavaScript object or array corresponds to an atomic result item type. That is, the underlying value type is
JS_OBJECT
orJS_ARRAY
, andItemType.isAtomic
returns true.A JSON node, such as the result of calling
cts.doc()
or the output from calling aNodeBuilder
method, has a node value type. That is, a value type such asOBJECT_NODE
,ARRAY_NODE
,BOOLEAN_NODE
,NUMBER_NODE
, orNULL_NODE
. Also,ItemType.isNode
returns true.
In most cases, your code can ignore this distinction because you can use Jackson to manipulate both kinds of results transparently through the JsonItem
interface. For details, see Working with JSON Content.