MarkLogic 12 Product Documentation
op.fromDocs

op.fromDocs(
   ctsQuery as cts.query,
   contextPath as String,
   columnSpecs as columnDef,
   [qualifier as String],
   [systemCols as String?],
   namespaces as Object?
) as AccessPlan

Summary

This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.

Parameters
ctsQuery Query to select documents for row generation . The query can be a cts.query or a string as a shortcut for a cts.wordQuery.
contextPath XPath applied to each matched document; each result becomes a row.
columnSpecs The column definitions create by using op.columnBuilder
qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
systemCols An optional named fragment id column returned by op.fragmentIdCol. One use case for fragment ids is in joins with lexicons or document content.
namespaces Namespaces prefix (key) and uri (value).

Usage Notes

The fromDocs function is one of the Data Access Functions.

Example

//Insert data
'use strict';
declareUpdate();
xdmp.documentInsert("test1.json",{id:1,name:"Bob",vector:"[1,2,3]",geo:cts.point(0.373899653086420E+02, -0.122078578406509E+03)},{collections:"fromDocsJson"})
xdmp.documentInsert("test2.json",{ name:"Alice",vector:"[2,3,4]"},{collections:"fromDocsJson"})
xdmp.documentInsert("test3.json",{id:3,name:"Mark"},{collections:"fromDocsJson"})
xdmp.documentInsert("test4.json",{id:4,name:"Cindy",vector:"[1,2]"},{collections:"fromDocsJson"})

//Run fromDocs
'use strict';
const op = require('/MarkLogic/optic');

op.fromDocs(
  cts.collectionQuery("fromDocsJson"),
  "/",
  op.columnBuilder()
   .addColumn("id").nullable(true).type("integer").default(-1)
   .addColumn("name").collation("http://marklogic.com/collation/codepoint")
   .addColumn("node").type("node").xpath(".")
   .addColumn("expr").type("unsignedLong").expr(op.xdmp.random())
   .addColumn("vector").type("vector").dimension(3)
   .addColumn("geoData").xpath("geo").type("point").coordinateSystem("wgs84/double").units("miles"),
 "myView", "myFragmentId"
)
.orderBy("id")
.select(["id","name","node","expr","vector","geoData","myFragmentId"])
.result()
  

Example

const op = require('/MarkLogic/optic');

op.fromDocs(
  cts.collectionQuery("test"),
  "/my:root",
  ,op.columnsBuilder()
    .addColumn("test") // add column test with type string xpath="test"
    .addColumn(op.col("test1")) // add column test2 with type string xpath="test2"
    .addColumn("test2").xpath("my:val2").type("")
    .addColumn("test3").expr(op.xpath(op.context(),"my:val3"))
    .addColumn("test4").expr(op.xpath(op.context(),"my:val4",{my:"http://www.my.com/override"}))
    .addColumn("test5").expr(op.param("inputValue"))
,  "MyView",
  "myFragmentId",
  {my:"http://www.my.com/my"},
)
  
Powered by MarkLogic Server | Terms of Use | Privacy Policy