Recently, using MongoDB, it is found that MONGO's definition of field type is not very strict, it relies entirely on the type of incoming data, and the addition of PHP is a weak type of language, so there will inevitably be some errors. If the expected type is int, but the data is stored in string, then the data is completely invalidated when retrieved by the INT type. What happens if a field is found to be different when it is stored? Of course, first of all to correct the application of data in the time of the error, for the int type must ensure that its type is correct.
Example
123 |
$user_arr = array ( ‘uid‘ => intval ( $_SESSION [ "user" ][ "id" ]), //对于Int类型的数据,一定要保证其类型的正确性! ); |
How do you fix the difference between existing data types?!
Query the data for all field types that are string, and then convert the string to int:
1234567 |
db.you_collection_name.find( ‘uid‘ : { $type : 2 }).forEach( function (x) { x.uid = parseInt(x.uid) ; //将String转为Int db.you_collection_name.save(x); } ) |
For the definition of the MongoDB field type, refer to: http://docs.mongodb.org/manual/reference/operator/type/#_S_type
Transfer from http://www.wangwei.info/2013/07/mongodb-type-conversion/
MongoDB Field type conversions