op:join-doc

op:join-doc(
   $plan as map:map,
   $docCol as item(),
   $sourceCol as item()
) as map:map

Summary

This function specifies a document column to add to the rows by reading the documents for an existing source column having a value of a document uri (which can be used to read other documents) or a fragment id (which can be used to read the source documents for rows).

As long as the values of the column are the same as document uris, the document join will work. If the document doesn't exist or the uri or fragment id is null in the row, the row is dropped from the rowset.

You should minimize the number of documents retrieved by filtering or limiting rows before joining documents.

Parameters
$plan The Optic Plan. You can either use the XQuery => chaining operator or specify the variable that captures the return value from the previous operation.
$docCol The document column to add to the rows. This can be a string or column specifying the name of the new column that should have the document as its value.
$sourceCol The document uri or fragment id value. This is either the output from op:fragment-id-col specifying a fragment id column or a document uri column. Joining on a fragment id is more efficient than joining on a uri column.

Example

xquery version "1.0-ml";

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $tab1 := op:from-literals((
   map:entry("id", 1) => map:with("uri", "/employee1.json"),
   map:entry("id", 2) => map:with("uri", "/employee2.json"),
   map:entry("id", 3) => map:with("uri", "/employee3.json"),
   map:entry("id", 4) => map:with("uri", "/employee20.json")
   ))
return $tab1
   => op:join-doc(op:col("doc"), op:col("uri"))
   => op:order-by("id")
   => op:result()
  

Example

xquery version "1.0-ml";

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $employees := op:from-view('main', 'employees', (),
                                op:fragment-id-col('docId'))
let $expenses := op:from-view('main', 'expenses', (),
                                op:fragment-id-col('docId2'))

return $employees
   => op:join-inner($expenses, op:on(
                op:view-col('employees', 'EmployeeID'),
                op:view-col('expenses', 'EmployeeID')))
   => op:join-doc('Employee', op:fragment-id-col('DocId'))
   => op:join-doc('Expenses', op:fragment-id-col('DocId2'))
   => op:select((op:view-col('employees', 'EmployeeID'),
                'FirstName', 'LastName',
                op:view-col('expenses', 'Category'),
                op:view-col('expenses', 'Amount'),
                'Employee', 'Expenses'))
   => op:order-by(op:view-col('employees', 'EmployeeID'))
   => op:result()

(: Returns the 'employee' and 'expense' source documents after the row data. :)
  

Example

xquery version "1.0-ml";

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $employees := op:from-view('main', 'employees', (),
                                op:fragment-id-col('docId'))
let $expenses := op:from-view('main', 'expenses', (),
                                op:fragment-id-col('docId2'))

return $employees
   => op:join-inner($expenses, op:on(
                op:view-col('employees', 'EmployeeID'),
                op:view-col('expenses', 'EmployeeID')))
   => op:join-doc-uri(op:col('employeeURI'), op:fragment-id-col('docId'))
   => op:join-doc-uri(op:col('expensesURI'), op:fragment-id-col('docId2'))
   => op:join-doc('Employee', op:col('employeeURI'))
   => op:join-doc('Expenses', op:col('expensesURI'))
   => op:select((op:view-col('employees', 'EmployeeID'),
                'FirstName', 'LastName',
                op:view-col('expenses', 'Category'),
                op:view-col('expenses', 'Amount'),
                'Employee', 'Expenses'))
   => op:order-by(op:view-col('employees', 'EmployeeID'))
   => op:result()

(: Returns the 'employee' and 'expense' source documents after the row data. :)
  
Powered by MarkLogic Server | Terms of Use | Privacy Policy