Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
cts query to find unique values from a field from json document in Marklogic staging

I'm trying to write a JavaScript cts query to query only unique values from a key from a JSON document based on another key. i.e, similar to a query like: select distinct(name) from data-hub-staging where source='source1'

{
    "source": "source1",
    "name": "John",
    "DOB": "1-01-1990",
    "load_date": "2021-10-23 10:23:55"
},
{
    "source": "source1",
    "name": "John",
    "DOB": "1-01-1990",
    "load_date": "2021-10-24 10:23:55"
},
{
    "source": "source1",
    "name": "Mark",
    "DOB": "1-01-1990",
    "load_date": "2021-10-24 10:23:55"
}

I have been trying the below query, but it returns all the fields. I wanted only the unique name field.

const query = cts.jsonPropertyValueQuery(
              "source",
              "source1");

[...new Set (cts.search(query)
.toArray()
  .map(doc => doc.root.name).sort())]

Current result: [John,John,Mark]
Expected result: [John,Mark]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The name values are not being de-duplicated by the Set because they are text() nodes. You need to get the .valueOf() those text nodes and then the strings will be de-duplicated.

const query = cts.jsonPropertyValueQuery(
              "source",
              "source1");

[...new Set (cts.search(query)
.toArray()
  .map(doc => doc.root.name.valueOf()).sort())]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!