Search code examples
phpcouchbasecouchbase-view

How to order results from CouchbaseViewQuery on Couchbase PHP SDK 2.0?


I started studying both Couchbase and MongoDB to decide wich one to implement on a social network, but the lack of documentation on couchbase side is almost making me give up.

Almost everything I must guess, as documentation are poor, and easier to get confuse between PHP SDK 2.0 and previous versions. There is a lot of documentation but about older sdk versions.

http://docs.couchbase.com/sdk-api/couchbase-php-client-2.0.2/index.html

Now after my outflow, my question.

I have this code, and the necessary view created:

$cb = CouchbaseViewQuery::from('dev_testimonials', 'by_uid')->key($uid)->limit($max)->skip($inicio);

It works as expected, except that I need to order the results by ascending or descending, but I could't find anywhere documentation about that. I thought ->descending(true) should do the trick but doesn't work. Doesn't exist.

All that the API reference says about ordering on CouchbaseViewQuery, is a list of constants:

UPDATE_BEFORE, UPDATE_NONE, UPDATE_AFTER, ORDER_ASCENDING, ORDER_DESCENDING

But there is not explanation about how and where to use them.

Could you help? Thanks.


Solution

  • The function you need to use is order() which accepts one of two constants:

    • ORDER_ASCENDING
    • ORDER_DESCENDING

    In php all class constants are publicly visible. To access the constants the following code can be used: CouchbaseViewQuery::ORDER_ASCENDING or CouchbaseViewQuery::ORDER_DESCENDING.

    Below is a code example using the Beer-sample data shipped with Couchbase Server.

    <?php
    // Connect to Couchbase Server
    
    $cluster = new CouchbaseCluster('http://127.0.0.1:8091');
    $bucket = $cluster->openBucket('beer-sample');
    $query = CouchbaseViewQuery::from('beer', 'by_location')->skip(6)->limit(2)->reduce(false)->order(CouchbaseViewQuery::ORDER_ASCENDING);
    $results = $bucket->query($query);
    
    foreach($results['rows'] as $row) {
            var_dump($row['key']);
    }
    
    echo "Reversing the order\n";
    
    $query = CouchbaseViewQuery::from('beer', 'by_location')->skip(6)->limit(2)->reduce(false)->order(CouchbaseViewQuery::ORDER_DESCENDING);
    $results = $bucket->query($query);
    
    foreach($results['rows'] as $row) {
            var_dump($row['key']);
    }
    

    Here is the output from the above code:

    array(3) {
      [0]=>
      string(9) "Australia"
      [1]=>
      string(15) "New South Wales"
      [2]=>
      string(6) "Sydney"
    }
    array(3) {
      [0]=>
      string(9) "Australia"
      [1]=>
      string(15) "New South Wales"
      [2]=>
      string(6) "Sydney"
    }
    Reversing the order
    array(3) {
      [0]=>
      string(13) "United States"
      [1]=>
      string(7) "Wyoming"
      [2]=>
      string(8) "Cheyenne"
    }
    array(3) {
      [0]=>
      string(13) "United States"
      [1]=>
      string(7) "Wyoming"
      [2]=>
      string(6) "Casper"
    }