Saahil Gupta
Saahil Gupta

Reputation: 209

how to query two databases in a single query in marklogic

I am replicating data from one topology to another (say data of topology 1 is in DB1 and data of topology 2 is in DB 2) and I want to perform a check on both the databases in a single query to check whether all the files are same or not either by checking their Uris or Hash values. Is that possible?

Upvotes: 1

Views: 443

Answers (1)

grtjn
grtjn

Reputation: 20414

A check using hash performs well only when you calculate the hash upfront, and store that inside the document or in its properties. But a check based on uris is pretty straight-forward. The following solution doesn't scale endlessly, but should work well up to a few million uris. Just copy-paste it in QConsole, and run it against the database of your choice, and change the $other-db-name value to the name of the database to compare against:

xquery version "1.0-ml";

let $other-db-name := "OtherDocuments"
return
xdmp:eval(
  '
    xquery version "1.0-ml";

    declare variable $uris as map:map external;

    "In other, but not here:",
    map:keys(cts:uris((), "map") - $uris),

    "Here, but not in other:",
    map:keys($uris - cts:uris((), "map"))
  ',
  (
    xs:QName("uris"), cts:uris((), "map")
  ),
  <options xmlns="xdmp:eval">
    <database>{xdmp:database($other-db-name)}</database>
  </options>
)

HTH!

Upvotes: 3

Related Questions