Problem

Find binary documents that have been recently modified.

Solution

Applies to MarkLogic versions 7+

xquery version "1.0-ml";
 
declare namespace qry  = "https://marklogic.com/cts/query";
 
let $binary-term := xdmp:plan(/binary())//qry:term-query/qry:key/text()
let $query-start := (fn:current-dateTime() - xs:dayTimeDuration("P1D"))
let $query-stop := fn:current-dateTime()
let $query := cts:and-query((
  cts:properties-query(
    cts:and-query((
      cts:element-range-query(xs:QName("prop:last-modified"), ">", $query-start),
      cts:element-range-query(xs:QName("prop:last-modified"), "<", $query-stop)
    ))
  ),
  cts:term-query($binary-term)
))
return (
  text{"Estimate:", xdmp:estimate(cts:search(fn:doc(), $query))},
  cts:uris((), ("limit=100"), $query)
)

Required Privileges:

  • https://marklogic.com/xdmp/privileges/xdmp-plan

Required Indexes:

  • “maintain last modified” must be on
  • dateTime range index on prop:last-modified

Discussion

Recently-modified binaries can be found if the “maintain last modified” option on the target database is active. You must also have a dateTime range index set up on prop:last-modified, so that the cts:element-range-querys will work.

The xs:dayTimeDuration chosen for $query-start defines what “recent” means in this case. Notice that the last-modified date is stored in a property fragment, so this recipe uses a cts:properties-query to look for it.

The recipe returns an estimate of the number of recently-modified binaries, as well as the URIs of the first 100. We can count on the estimate to be accurate, as the query is targeting indexes.

Learn More

Find Binaries

Learn how to quickly find the URIs of binary documents.

Developer's Guide

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

MarkLogic Developer Track

Want to build that awesome app? Get off the ground 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.