uttamkini
uttamkini

Reputation: 508

How can I get a document based on the latest date

I am quite new to Marklogic and xquery in general. We have a collection of documents which have a deadline date as a node. Can somebody help me with an xquery to get the latest document based on deadline date? Each document looks like

<document>
<Id>blah<Id>
<DeadlineDate>2012-04-04T21:00:00</DeadLineDate>

Upvotes: 1

Views: 1363

Answers (1)

mblakele
mblakele

Reputation: 7840

You will need a range index on DeadlineDate, of type dateTime.

let $latest := cts:element-values(
  xs:QName('DeadlineDate'), ('document', 'descending', 'limit=1'))
return /document[DeadLineDate eq $latest]

Or you could also use this form:

(for $n in /document[DeadLineDate]
 order by $n/DeadLineDate descending
 return $n)[1]

I think the first one will usually be faster, but that might be worth testing.

Upvotes: 2

Related Questions