Skip to content

Commit 7cb2601

Browse files
Mariusmariusmarklogic
authored andcommitted
NODE-124: Add an example for using unnest functions
1 parent 4b4263a commit 7cb2601

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

examples/unnest-function.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const exutil = require('./example-util.js');
18+
const marklogic = require('../');
19+
const dbReader = marklogic.createDatabaseClient(exutil.restReaderConnection);
20+
21+
console.log('This example is a demonstration of unnest functions');
22+
23+
const op = marklogic.planBuilder;
24+
25+
// This schema and view should be in the database before running.
26+
const fromView = op.fromView("unnestSchema", "unnestView");
27+
28+
// This is a binding template for the tokenize function, which splits a comma-separated string into an array.
29+
const binding = op.as("teamMemberNameArray", op.fn.tokenize(
30+
op.col("teamMembers"),
31+
op.xs.string(",")
32+
));
33+
34+
// This specifies the column to order the results by.
35+
const orderByCol = op.col("teamMemberName");
36+
37+
// This creates a query plan that selects rows from the view, splits the teamMembers column into an array,
38+
// unnests the array into individual rows, and orders the results by teamMemberName.
39+
const planFromBuilderTemplate = fromView
40+
.bind(binding)
41+
.unnestInner("teamMemberNameArray", "teamMemberName")
42+
.orderBy(orderByCol);
43+
44+
// This executes the query plan and logs the results to the console.
45+
dbReader.rows.query(planFromBuilderTemplate)
46+
.then(r => console.log(r))
47+
.catch(e => console.log(e));

0 commit comments

Comments
 (0)