Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Why Mongodb sorting are wrong ?
  • The buy is not ascending order: 327.65  330.49  333.1  328.81  329.00
    you can see only a few record, the sorting is mass up .

    <?php
            $pairs = 'BTCUSD';

            $mongodb = \Mongo_Db::instance('trade');
            $mongodb->select(array(
                'exchange',
                'currency0',
                'buy',
                'sell',
                'spread'
            ));
            $mongodb->order_by(array(
                'buy' => 'asc'
            ));
            $mongodb->where(array('currency0' => $pairs));
            $c = $mongodb->get('fxquote');

            print('<pre>');
            print_r($c);
            print('</pre>');
    ?>

    Result:
    Array
    (
    [0] => Array
    (
    [_id] => 14191165849509-5027-7877
    [exchange] => okcoin
    [currency0] => BTCUSD
    [buy] => 327.65
    [sell] => 327.42
    [spread] => 0.070196856401636
    )

    [1] => Array
    (
    [_id] => 14191165829361-8202-7541
    [exchange] => coinsetter
    [currency0] => BTCUSD
    [buy] => 330.49
    [sell] => 328.46
    [spread] => 0.61423946261612
    )

    [2] => Array
    (
    [_id] => 14191165838565-6740-8801
    [exchange] => bitmex
    [currency0] => BTCUSD
    [buy] => 333.1
    [sell] => 329.05
    [spread] => 1.215851095767
    )

    [3] => Array
    (
    [_id] => 14191165838875-6710-7167
    [exchange] => bitfinex
    [currency0] => BTCUSD
    [buy] => 328.81
    [sell] => 328.11
    [spread] => 0.21288890240564
    )

    [4] => Array
    (
    [_id] => 14191165826589-1864-6206
    [exchange] => 796
    [currency0] => BTCUSD
    [buy] => 329.00
    [sell] => 328.67
    [spread] => 0.10030395136778
    )

    )
  • Have you tried enabling the profiler and see what it sends to Mongo?

    I can't see any immediate issue, it should add "buy":1 to the sorts list.
  • for quick and dirty fix... I add this code after the query  : (

            foreach ($c as $val) {
                $tmp[] = $val['buy'];
            }
            
            array_multisort($tmp, SORT_ASC, $c);

            return $c;
  • HarroHarro
    Accepted Answer
    That is not a fix, it's a workaround.

    If it is really a bug it needs to be fixed. But I can't imagine, that code hasn't changed in years, other people would have complained about that.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion