mongoDB按百分比排序 [英] mongoDB sort by percentage
问题描述
Ok我知道这可以做,但我面对的问题是我无法得到它的工作
百分比保存为0.03或0.05或0.10
我尝试通过
对它们进行排序。 $ cursor-> ; sort(array(offers=> array(percentage=> 0)));
我也试过了
$ cursor-> sort(array(offers=> array(cashback=> 1)));
,但没有任何更改。
可以请一些帮助
这是一个样本集合
array(
'_id'▼=> new MongoId(50513d8338fc5de706000000),
'test'=> new MongoInt32(1),
'offers'=&
array(
'0'=>
array(
'minspend'=> '50 .00',
'cashback'=>'1.50'
'percentage'=>'0.03',
),
'1'=>
array(
'minspend'=>'100.00'
'cashback'=>'3.00',
'percentage'=>'0.03',
),
),
) / code>
解决方案 Russell,
Sort用于排列整个文档的顺序。形式这看起来你试图在一个文档中排序数组。
要实现这一点,您需要检索整个文档,然后需要编写自己的排序函数客户端(PHP)端。
请查看使用PHP中的usort函数。
http://php.net/manual/en/function.usort.php
Ok i know this can be done, but the issue i face is all my attemps i cant get it to work
the percents are saved as 0.03 or 0.05 or 0.10
i tried to sort them by
$cursor->sort(array("offers"=>array("percentage"=>0)));
I have also tried
$cursor->sort(array("offers"=>array("cashback"=>1)));
but no changes.
could some one please help
Here is a sample collection
array (
'_id'▼ => new MongoId("50513d8338fc5de706000000"),
'test' => new MongoInt32(1),
'offers' =>
array (
'0' =>
array (
'minspend' => '50.00',
'cashback' => '1.50',
'percentage' => '0.03',
),
'1' =>
array (
'minspend' => '100.00',
'cashback' => '3.00',
'percentage' => '0.03',
),
),
)
解决方案 Russell,
Sort is used to arrange the order of whole documents. Form this you look to be attempting to sort arrays within a single document.
To accomplish this you will need to retrieve the whole document and then need to write your own sort function client (PHP) side.
Please have a look at using the usort function in PHP.
http://php.net/manual/en/function.usort.php
这篇关于mongoDB按百分比排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!