oracle-databaseoracle-soda

Oracle SODA - How to sort on created using REST?


I can't work out how to use the $orderby with SODA on an id field (such as created or lastModified. I'm using SODA for REST directly and not the other projects.

Sort syntax is:

{
  $orderby: {
    path: 'created',
    datatype: 'date',
    order: 'desc'
  }
}

And I've also tried:

{
    "$orderby": {
        "$fields": [{
                
            "path": "created",
            "datatype": "date",
            "order": "desc"
        }],
        "$scalarRequired": true
    }
}

And replacing the path with $id: 'created' (as you can use that in a filter specification to access non-document metadata. But nothing works to order properly.

Short of putting the created field into my object when I create them (which defeats the purpose of having those fields) how can I use orderby on a metadata field?


Solution

  • Max here from the SODA dev team. I am not 100% sure what you mean by an "id field". Looks like you mean the "created on" and "last modified" document components automatically maintained by SODA, right? If so, we don't support orderbys on these (though it could be added as an enhancement).

    As of now, as you mentioned in your post, best option is to create a field in your JSON documents' content and set it to ISO8601 format timestamp value (e.g. 2020-10-13T07:01:01). You can then do an orderby on such a field (with datatype "datetime"). Please let me know if more details on this are needed.

    In SODA REST, when you're listing collection contents, you could specify since=timestamp and until=timestamp query parameters. That'll give you all documents with last modified timestamp greater than the "since" one, and less than or equal to the "until" one.

    Example:

    http://host:port/ords/scott/soda/latest/myColl?since=2020:01:01T00:00:00&until=2021:01:01T00:00:00

    As part of this operation, SODA automatically adds an orderby on "last modified". Not sure if that's useful to you though, since that's just for listing all documents in the collection (i.e. you can't combine it with a QBE, for example). So if this doesn't meet your needs, best option right now is to explicitly add something like a "modified' field to the document content, and do an orderby on that.