Mongodb PHP - 带小数的整数

时间:2012-01-25 16:18:20

标签: php mongodb numbers decimal

这是关于在mongodb中插入整数的另一篇文章的后续文章。通过使用(int)我可以正常工作,但是现在当它将数字插入数据库时​​会删除小数点后的所有内容。因此,如果我有154.65,它将被插入到mongodb中,并且在十进制之后没有任何内容。

我的问题是我怎样才能保留小数点后的所有数字?此外,如果任何人有这些刺痛/数字功能的链接,我会很感激参考。感谢。

$number["xyz"]    = (int) $_POST["number"] ;
$collection->insert($number);

这会将其作为整数插入,但会删除小数点后的所有内容。我试过你的建议

$number["xyz"]    = floatval($_POST["number"]) ;
$collection->insert($number);

但这仍然会消除小数点后的所有内容。我虽然我需要看起来像这样的东西,所以它是mongo格式:

$number["xyz"]    = (dec) $_POST["number"] ;
$collection->insert($number);

3 个答案:

答案 0 :(得分:5)

没有本机支持的十进制格式。您的选项是浮点(在保存之前强制转换),整数(导致在您看到时删除小数)或字符串(在保存之前强制转换)。最佳选择取决于您的功能要求。例如,如果它是货币值,您不想使用浮点数,而是将值以美分存储为整数(在您的示例中为15465)。

答案 1 :(得分:5)

我会尽力为你们解决这个问题,因为在尝试对产品类别列表中的价格进行排序时,这有点令人难以置信。

我能够在mongo中生成十进制数的方式实际上是将值存储为float。

您还可以使用int或MongoInt64作为美分插入(之后除以100以返回美元)。这是我使用mongo php类的插入/导入代码。

$m = new Mongo();
$db = $m->selectDB("test");
$collection = new MongoCollection($db, 'numInt');
$collection2 = new MongoCollection($db, 'numFloat');
$collection3 = new MongoCollection($db, 'numLong');

$collection->insert(array('Integer' => (int)123.54));//I tried a decimal just to see...nope...gets cut off
$collection->insert(array('Integer' => (int)54321));
$collection->insert(array('Integer' => (int)12345));

$collection2->insert(array('Float' => (float)12354));
$collection2->insert(array('Float' => (float)54321));
$collection2->insert(array('Float' => (float)123.45));//*********This was stored as decimal in database results

$collection3->insert(array('Mongo64'=>new MongoInt64('54234.45')));//I tried a decimal just to see...nope...gets cut off
$collection3->insert(array('Mongo64'=>new MongoInt64('75432')));
$collection3->insert(array('Mongo64'=>new MongoInt64('12345')));


//results

//first example int and MongoInt64 (NumberLong in PHP) ***sorted ascending
echo "<strong>Integer Example</strong><br />
           --------------------<br />";

$cursor = $collection->find();
$cursor->sort(array('Integer'=>1));            
foreach($cursor as $obj){
   echo ($obj['Integer']/100)."<br />";
}

echo "<br />";
//second example float ***sorted ascending
echo "<strong>Float Example</strong><br />
              ----------------------------<br />";

$cursor2 = $collection2->find();
$cursor2->sort(array('Float'=>1)); 
foreach($cursor2 as $obj){
   echo ($obj['Float']/100)."<br />";
}

echo "<br />";
//third example float ***sorted ascending
echo "<strong>Number Long (MongoInt64) Example</strong><br />
              ------------------------------<br />";

$cursor3 = $collection3->find();
$cursor3->sort(array('Mongo64'=>1)); 
foreach($cursor3 as $obj){
   echo ($obj['Mongo64']/100)."<br />";
}

这是我在上面发布的php代码的输出....

Integer Example
--------------------
1.23
123.45
543.21

Float Example
----------------------------
1.2345
123.54
543.21

Number Long (MongoInt64) Example
------------------------------
123.45
542.34
754.32

以下是mongo shell中每个集合的find()结果

> db.numInt.find()
{ "_id" : ObjectId("50a322508c85671a2b000000"), "Integer" : 123 }
{ "_id" : ObjectId("50a322508c85671a2b000001"), "Integer" : 54321 }
{ "_id" : ObjectId("50a322508c85671a2b000002"), "Integer" : 12345 }
> db.numFloat.find()
{ "_id" : ObjectId("50a322508c85671a2b000003"), "Float" : 12354 }
{ "_id" : ObjectId("50a322508c85671a2b000004"), "Float" : 54321 }
{ "_id" : ObjectId("50a322508c85671a2b000005"), "Float" : 123.45 }
> db.numLong.find()
{ "_id" : ObjectId("50a322508c85671a2b000006"), "Mongo64" : NumberLong(54234) }
{ "_id" : ObjectId("50a322508c85671a2b000007"), "Mongo64" : NumberLong(75432) }
{ "_id" : ObjectId("50a322508c85671a2b000008"), "Mongo64" : NumberLong(12345) }
>

答案 2 :(得分:3)

通过打印出来看看$_POST["number"];实际给你的是什么。我觉得你的问题在于输入。所有这些都按预期工作:

$collection->insert(array("test"=>"a","float"=>123.45));
$collection->insert(array("test"=>"b","float"=>floatval("123.45")));
$test = array("test"=>"c","float"=>123.45);
$collection->insert($test);

结果:

db.test.find();
{ "_id" : ObjectId("4f2036a6eabc88dd0e000006"), "test" : "a", "float" : 123.45 }
{ "_id" : ObjectId("4f2036a6eabc88dd0e000007"), "test" : "b", "float" : 123.45 }
{ "_id" : ObjectId("4f2036a6eabc88dd0e000008"), "test" : "c", "float" : 123.45 }

为整数添加更多示例:

$collection->insert(array("test"=>"inta","int"=>new MongoInt32("12345")));
$collection->insert(array("test"=>"intb","int"=>new MongoInt64("123456789")));

导致(至少在我的系统上):

{ "_id" : ObjectId("4f20431feabc88cf3500001b"), "test" : "inta", "int" : 12345 }
{ "_id" : ObjectId("4f20431feabc88cf3500001c"), "test" : "intb", "int" : NumberLong(123456789) }

所以,如果你想在mongodb中将数字存储为“真正的整数”,你必须要小心。

<强>更新 10gen的某个人纠正了我的错误,即PHP中的整数通常作为整数存储在mongo中,即使shell中的类型显示为Number。还有一个mongo PHP驱动程序的设置,允许您指定此行为“native_long”。 http://php.net/manual/en/mongo.configuration.php

相关问题