This is a follow up to another post i made about inserting an integer into mongodb. I have this working just fine by using the (int) but now when it inserts the number into
I'll try to clear this up for you guys because this was boggling my mind for a bit when trying to sort prices in product category listings.
The way I was able to produce decimal numbers in mongo was in fact to store the values as float.
You can also insert as cents with int or MongoInt64 (divide by 100 afterwards to get back to dollars). Here is my insert/import code using the mongo php class.
$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 "Integer Example
--------------------
";
$cursor = $collection->find();
$cursor->sort(array('Integer'=>1));
foreach($cursor as $obj){
echo ($obj['Integer']/100)."
";
}
echo "
";
//second example float ***sorted ascending
echo "Float Example
----------------------------
";
$cursor2 = $collection2->find();
$cursor2->sort(array('Float'=>1));
foreach($cursor2 as $obj){
echo ($obj['Float']/100)."
";
}
echo "
";
//third example float ***sorted ascending
echo "Number Long (MongoInt64) Example
------------------------------
";
$cursor3 = $collection3->find();
$cursor3->sort(array('Mongo64'=>1));
foreach($cursor3 as $obj){
echo ($obj['Mongo64']/100)."
";
}
This is the output from the php code I posted above....
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
Here were the results of a find() for each of the collections in the mongo shell
> 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) }
>