MongoDB Field type conversions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.