Loading TOC...

MarkLogic Server 11.0 Product Documentation
AccessPlan.prototype.where

AccessPlan.prototype.where(
   filterExpression as filterDefinition
) as ModifyPlan

Summary

This method restricts the row set to rows matched by the boolean expression. Use boolean composers such as op.and and op.or to combine multiple expressions.

A constraining document query returns only the rows from the matched source documents. If the constraining document query is a node instead of a cts.query object, the implementation calls the cts.query parser on the node. The constraining document query applies to all upstream accessors. A constraining sem.store returns only the triples from the specified store (potentially expanded by inference using a ruleset). A constraining sem.store applies to all upstream triples accessors.

Parameters
filterExpression This can be a boolean expression, a cts.query to qualify the source documents that produced the rows set, or (where part of the row set was produced by the op.fromTriples accessor) a sem.store to restrict or expand the triples that produce the row set.

Usage Notes

where is a method of the following classes:

Example

// Locate employee expenses that exceed the allowed limit

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

const employees = op.fromView('main', 'employees');
const expenses = op.fromView('main', 'expenses');
const expenselimit = op.fromView('main', 'expenselimit');

const Plan =
employees.joinInner(expenses, op.on(employees.col('EmployeeID'), expenses.col('EmployeeID')))
         .joinInner(expenselimit, op.on(expenses.col('Category'), expenselimit.col('Category')))
         .where(op.gt(expenses.col('Amount'), expenselimit.col('Limit') ))
         .select([employees.col('EmployeeID'), 'FirstName', 'LastName', expenses.col('Category'),
                  expenses.col('Amount'),  expenselimit.col('Limit') ])
         .orderBy(employees.col('EmployeeID'))
   Plan.result();

  

Example

// Insert the template for view "docWhereExample"
declareUpdate();
const tde = require("/MarkLogic/tde.xqy");
const template = {
  "template":{
    "context":"/",
    "collections": ["docWhereExample"],
    "rows":[
      {
        "schemaName":"docWhereExample",
        "viewName":"docWhereExample",
        "columns":[
          {
            "name":"id",
            "scalarType":"long",
            "val":"./id"
          },
          {
            "name":"name",
            "scalarType":"string",
            "val":"./name"
          }
        ]
      }
    ]
  }
}
tde.templateInsert("docWhereTemplate.json", template);

//Insert documents
declareUpdate();
const docs = [{
   uri: "john.json",
   id : 1,
   name: "john",
   sentence : "John is there"
},
{
   uri: "joe.json",
   id : 1,
   name: 'joe',
   sentence : "Joe was there"
},
{
   uri: "alice.json",
   id : 2,
   name: 'alice',
   sentence : "Alice was there"
},
];
for ( let doc of docs ) {
xdmp.documentInsert(doc.uri, doc, {collections:"docWhereExample"})
}

//Below query returns "joe"
const op = require('/MarkLogic/optic');
op.fromView('docWhereExample', 'docWhereExample')
   .where(op.eq(op.col("id"),1))
   .where(cts.jsonPropertyWordQuery("sentence","was"))
   .select(["name"])
   .result();
  

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.