Hi Rachel,

You're right, that's my mistake. As Michael Blakeley pointed out (later in the 
thread), the type is irrelevant in this case.

Thanks.

-jb



From: Rachel Wilson <rachel.wil...@bbc.co.uk<mailto:rachel.wil...@bbc.co.uk>>
Reply-To: MarkLogic Developer Discussion 
<general@developer.marklogic.com<mailto:general@developer.marklogic.com>>
Date: Thursday, October 2, 2014 at 2:09 PM
To: MarkLogic Developer Discussion 
<general@developer.marklogic.com<mailto:general@developer.marklogic.com>>
Subject: Re: [MarkLogic Dev General] order by clause ignored?

Hi jb,

Ah yes, I was aware of that one having being bitten by it before :)  But in 
this case it doesn't make sense because even as strings they'd be ordered 
correctly, no?

From: Joe Bryan <joe.br...@marklogic.com<mailto:joe.br...@marklogic.com>>
Reply-To: MarkLogic Developer Discussion 
<general@developer.marklogic.com<mailto:general@developer.marklogic.com>>
Date: Thursday, 2 October 2014 17:34
To: MarkLogic Developer Discussion 
<general@developer.marklogic.com<mailto:general@developer.marklogic.com>>
Subject: Re: [MarkLogic Dev General] order by clause ignored?

Hi Rachel,

The order by clause is type-aware. You example orders by the data() value of 
the provided element (which will be xs:string unless a schema is in scope). You 
should construct an xs:dateTime and order by that:

order by xs:dateTime($x/publicationDateTime)

It's also worth noting that map:map is an unordered collection of entries. You 
can use a json:array instead if you need to preserve the order of your 
collection.

Thanks.

-jb

From: Rachel Wilson <rachel.wil...@bbc.co.uk<mailto:rachel.wil...@bbc.co.uk>>
Reply-To: MarkLogic Developer Discussion 
<general@developer.marklogic.com<mailto:general@developer.marklogic.com>>
Date: Thursday, October 2, 2014 at 12:17 PM
To: MarkLogic Developer Discussion 
<general@developer.marklogic.com<mailto:general@developer.marklogic.com>>
Subject: [MarkLogic Dev General] order by clause ignored?

I realise this is more of an xquery question but everyone here is stumped :-s

I have a query that is trying to calculate some earliest and latest dates that 
an event occurred.  The events are represented in xml like this:

<publication>
  <publicationDateTime>2013-08-14T15:06:51.921302Z</publicationDateTime>
  <documents>
    <document>
        <id>b119c2a3-5436-59d2-9771-a67f8e2b4172</id>
        <version>1</version>
    </document>
</documents>
</publication>

There are two events for the this particular document (i.e. with the same 
document id), and therefore two publication documents on different days.  In 
order to work out the first and last dates I'm sorting all my publication docs 
by publication date, then taking the sorted publication documents in order I'm 
iterating through the publication document ids in order and remembering them in 
the correct maps for earliest seen and latest seen dates.  However the results 
in the map are as if the publication documents are coming out in document order.

If I run this query the document dates are coming out in the right order as I'd 
expect:
------------------------------------------------------------------
let $earliestPubDates := map:map()
let $latestPubDates := map:map()
let $buildMaps :=
   for $x in doc()/publication
   let $publishedDate := $x/publicationDateTime/string()
   let $documents := $x/documents/document
   order by $x/publicationDateTime
   return
     (: map building stuff commented out in order to see how the dates are 
being ordered :)
     $publishedDate

return $buildMaps
---------------------------------------------------------------------
returns:
2013-08-14T15:06:51.921302Z
2014-09-03T14:46:07.757612Z



However, after someone showed me how to debug the loop a little with an error 
statement, I'm finding that the first publication processed is the later one, 
not the earlier one.  It looks like the order by in this case is being ignored. 
 Could someone tell me how and why?



----------------------------------------------------------------------------
let $earliestPubDates := map:map()
let $latestPubDates := map:map()

let $buildMaps :=
   for $x in doc()/publication
   let $documents := $x/documents/document
   order by $x/publicationDateTime
   return
      for $contentId in $documents/id/string()
      let $publishedDate := $x/publicationDateTime/string()
      let $putEarliest := if (not(map:contains($earliestPubDates, $contentId))) 
then (
                             let $dummy := map:put($earliestPubDates, 
$contentId, $x/publicationDateTime)
                             let $a := error("Adding earliest published date " 
|| $x/publicationDateTime)
                             return $dummy
                          ) else ()
      let $putLatest := map:put($latestPubDates, $contentId, $publishedDate)
      return ()

return $buildMaps
----------------------------------
Returns (in an error stack trace): "Adding earliest published date 
2014-09-03T14:46:07.757612Z"


Is it something to do with when the order by is applied to the statement?

Any help gratefully received
Rachel
_______________________________________________
General mailing list
General@developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to