Search code examples
phpmongodbtypessaving-data

How can I ensure a specific datatype when saving in mongo?


I'm using mongodb.

I'm saving a record with a very long number, but I don't want to save it as a number (a float number) I want it as a string.

e.g. I saved "171829572137423434" and I got "1.718295E+16" (just an example) But I need the complete number since is an ID, how can I force to save it as a string in mongodb?

By the way I am using PHP api.


Solution

  • $number = 42;
    
    // this stores as a number
    $collection->insert(array('number'=>$number));
    
    // this stores as a string
    $collection->insert(array('number'=>strval($number)));