roytang.net Posts Photos Archives About

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: {
    count: 100,
    total: 10000,
    summaryDate: (date value)
  }
}

However, not all documents have this property. I'd like to construct an SJS query to retrieve those documents that don't have this property defined. If it was SQL, I guess the equivalent would be something like "WHERE summaryData IS NULL"

I wasn't sure what to search for in the docs. Any advise would be helpful.

Comments

David Ennis said...

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 https://stackoverflow.com/questions/46076264/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.