Mongodb PHP-带小数的整数

Mongodb PHP-带小数的整数,php,mongodb,numbers,decimal,Php,Mongodb,Numbers,Decimal,这是我关于在mongodb中插入整数的另一篇文章的后续文章。通过使用(int)我可以很好地实现这一点,但是现在当它将数字插入到db中时,它会删除小数点后的所有内容。因此,如果我有154.65,它将作为154插入mongodb,小数点后没有任何内容 我的问题是,如何使所有数字保持在小数点后?另外,如果有人有这些sting/numeric函数的链接,我希望能提供参考。谢谢 $number["xyz"] = (int) $_POST["number"] ; $collection->ins

这是我关于在mongodb中插入整数的另一篇文章的后续文章。通过使用(int)我可以很好地实现这一点,但是现在当它将数字插入到db中时,它会删除小数点后的所有内容。因此,如果我有154.65,它将作为154插入mongodb,小数点后没有任何内容

我的问题是,如何使所有数字保持在小数点后?另外,如果有人有这些sting/numeric函数的链接,我希望能提供参考。谢谢

$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);

没有本机支持的十进制格式。您的选项有浮点(保存前强制转换)、整数(如您所见,它会删除小数)或字符串(保存前强制转换)。最佳选择取决于您的功能需求。例如,如果它是一个货币值,您不希望使用浮动,而是将该值以美分为单位存储为整数(在您的示例中为15465)。

请参阅what
$\u 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")));
结果(至少在我的系统上):

因此,如果您想在mongodb中将数字存储为“实整数”,就必须稍微小心一点

更新
10gen的人纠正了我的错误,PHP整数通常在mongo中存储为整数,即使shell中的类型显示Number。mongo PHP驱动程序还有一个设置,允许您将此行为指定为“native_long”

我会尽力为你们澄清这一点,因为在尝试对产品类别列表中的价格进行排序时,这让我有点困惑

我在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 />";
}
以下是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) }
>

我想知道有多少生产代码使用浮点作为货币值;)也许您应该去掉“没有本机支持的十进制格式”这句话,因为mongo和php都提供本机十进制格式:
Number
用于mongo(这是所有数字文本的默认值)和
float
用于php。必须显式创建MongoInt64以在php中存储为NumberLong(mongo中的整数格式)。显然,我是个新手,正在努力学习最佳实践。谢谢你的建议。我认为使用整数听上去是赚钱/赚钱的最佳途径。谢谢。@Wes我不明白你的意思。BSON(因此MongoDB)不支持与DECIMAL等效的类型(即具有固定小数数的整数类型)。我误解你了吗?
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
> 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) }
>