Skip to content Skip to sidebar Skip to footer

Marklogic - Query For Documents Where A Specific Json Property Is Not Defined

I'm using ML8. I have a bunch of json documents in the database. Some documents have a certain property 'summaryData', something like: { ...(other stuff)... summaryData: { co

Solution 1:

You can find the existence of a JSON property in a document by using cts.jsonPropertyScopeQuery() and the second parameter set to cts.trueQuery()

To find the opposite, you can wrap that part of your query in cts.notQuery()

Example:

cts.search(
  cts.notQuery(
    cts.jsonPropertyScopeQuery('summaryData', cts.trueQuery())
  )
)

Example inside of a larger query for clarity (or more confusion.. who knows.. :)

cts.search(
  cts.andQuery([
     cts.directoryQuery('/some/scoping/path/'), 
     cts.notQuery(
        cts.jsonPropertyScopeQuery('myMissingElement', cts.trueQuery())
     )
 ])
)

This is somewhat explained in the cts.elementQuery() documentation.

Lastly: one may argue that this is a duplicate of Need XQuery syntax for 'if Exists()' behaviour in search api I was going to mark it as a duplicate, but did not because you asked about SJS, a property and to negate the search. Someone else may differ in opinion and mark it as duplicate.

Post a Comment for "Marklogic - Query For Documents Where A Specific Json Property Is Not Defined"