2012-01-25 53 views
3

這是我關於將一個整數插入到mongodb中的另一篇文章的後續內容。通過使用(int),我已經可以正常工作了,但現在當它將數字插入數據庫時​​,會剝離小數點後的所有內容。所以如果我有154.65它將被插入到mongodb中作爲154,小數點後面沒有任何東西。Mongodb PHP - 帶小數點的整數

我的問題是我如何得到這個保留小數點後的所有數字?此外,如果任何人有鏈接到這些sting /數字函數,我會很感激的參考。謝謝。

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

這插入它作爲一個整數,但剝去小數點後的所有內容。我試過你的建議

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

但這仍然剝去小數點後的所有東西。我雖然我需要的東西,看起來像這樣使得它在蒙戈格式:

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

回答

5

有沒有原生支持十進制格式。您的選項是浮點數(在保存之前進行轉換),整數(這會導致您刪除小數點)或字符串(在保存之前進行轉換)。最佳選擇取決於您的功能要求。例如,如果它是一個貨幣值,您不希望使用浮動,但以美分爲單位將值存儲爲整數(在您的示例中爲15465)。

+1

+1,建議提交貨幣值存儲爲整數。 –

+1

我想知道有多少產品代碼使用浮點數來計算貨幣值;) –

+0

也許你應該取出「no natively supported decimal format」這個句子,因爲mongo和php都提供了原生的十進制格式:'Number' for Mongo(這是默認爲所有數字文字)和「浮動」的PHP。你必須明確地創建MongoInt64以在PHP中存儲爲NumberLong(整數格式爲mongo)。 http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions –

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中將數字存儲爲「真正的整數」,要稍微小心。

更新 我被人在該PHP整數10gen的修正通常存儲在蒙戈整數,即使在shell類型顯示號碼。還有一個mongo PHP驅動程序的設置,允許您指定「native_long」行爲。 http://php.net/manual/en/mongo.configuration.php

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 

這裏是一個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) } 
>