Home > Backend Development > PHP Tutorial > mongo如何针对浮点数排序

mongo如何针对浮点数排序

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:12:17
Original
1059 people have browsed it

<code>   $result = array('limit'=>100,'order'=>array('price'=>-1));
   $fields = array('_id'=>false,'id'=>true);
   $list = $this->classMod->getList($condition,$result,$fields);
   
   </code>
Copy after login
Copy after login

mongo如何针对浮点数排序

<code>    这是现有的排序结果。。不知道如何能够针对浮点排序</code>
Copy after login
Copy after login

回复内容:

<code>   $result = array('limit'=>100,'order'=>array('price'=>-1));
   $fields = array('_id'=>false,'id'=>true);
   $list = $this->classMod->getList($condition,$result,$fields);
   
   </code>
Copy after login
Copy after login

mongo如何针对浮点数排序

<code>    这是现有的排序结果。。不知道如何能够针对浮点排序</code>
Copy after login
Copy after login

你的price是字符串,不是浮点数。查看你的驱动说明,怎么才能把数字保存成为浮点数。
MongoDB不支持Decimal类型,所以一些驱动遇到decimal的时候都是保存成为字符串以避免丢失精度。但是这也有很明显的短片,变成字符串排序就有问题了。可行的解决方案包括:

  1. 使用其他类型

  2. 同时使用decimal和float保存两个字段,排序时用float,取值时用decimal。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template