I am unable to get the ORDER BY to work correctly in Couchbase Server 4.5
Documents
{ "name": "Green", "price": "156" }
{ "name": "Yellow", "price": "175" }
{ "name": "Red", "price": "1" }
{ "name": "Blue", "price": "18" }
PHP
$cluster = new CouchbaseCluster('http://127.0.0.1:8091');
$bucket = $cluster->openBucket('products');
$bucket->enableN1ql(array('http://127.0.0.1:8093'));
$query = CouchbaseN1qlQuery::fromString("SELECT * FROM products ORDER BY price ASC");
$results = $bucket->query($query);
foreach ($results as &$r) {
$name = $r->products->name;
$price = $r->products->price;
echo "$name: $price <br>";
}
Output
Red: 1
Green: 156
Yellow: 175
Blue: 18
Please will you let me know how I can tell Couchbase that the price field is an integer so the order will be done correctly.
Assume the documents are in bucket a. Then the following query should work.
select to_number(price) b from a order by b;
{
"requestID": "6642f123-08c1-4ec2-9380-c11c9fd05c60",
"signature": {
"b": "number"
},
"results": [
{
"b": 1
},
{
"b": 18
},
{
"b": 156
},
{
"b": 175
}
],
"status": "success",
"metrics": {
"elapsedTime": "9.39578ms",
"executionTime": "9.36118ms",
"resultCount": 4,
"resultSize": 125,
"sortCount": 4
}
}