Problem

You want to find all JSON documents that are missing a particular property.

Solution

Applies to MarkLogic versions 8+

cts.search(
  cts.notQuery(
    cts.jsonPropertyScopeQuery(
      "target", 
      cts.trueQuery()
    )
  )
)
cts:search(
  fn:doc(),
  cts:not-query(
    cts:json-property-scope-query(
      "targetElement",
      cts:true-query()
    )
  )
)

Discussion

MarkLogic’s built-in search engine uses query criteria to identify matching fragments. The indexes map terms (words, phrases, structures, etc.) to fragment identifiers. To run a search, the specified terms are looked up in the appropriate index to find fragment identifiers. In the case of cts:not-query, the search will return any fragment identifiers except those matched by the nested query.

cts:json-property-scope-query is a useful way to constrain a search to part of a document. The function restricts the nested query to matching within the specified JSON property.

The cts:true-query query passed to cts:json-property-scope-query does what it sounds like — it matches everything. Passed to cts:json-property-scope-query, this provides a simple way to test for the existence of an element.1

Learn More

Find documents that do NOT have an element

Learn how to find all XML documents that are missing a particular element.

Application Developer's Guide

Read the extensive guide that reviews over application development in MarkLogic Server.

MarkLogic Developer Track

Want to build that awesome app? Get off the ground quickly with the MarkLogic developer track.

This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.