MongoDB Data Types


All databases have their own data types, and MongoDB is no different. A summary of MongoDB’s available data types are as follows:

  • ObjectId: Also known within PHP as MongoId, is a unique object usually provided as a primary key by default in the property _id. It is 12 bytes long, and is automatically created by the database when you insert a document without an _id property set. You can also set your own values, but remember that this is used as the primary key and so must be unique.
  • String: Just like strings in PHP, however all strings must be UTF-8. You need to convert non-UTF-8 strings before inserting into your database.
  • Binary: Used for non-UTF-8 strings and other binary data.
  • Boolean: You can use the familiar TRUE and FALSE right from PHP.
  • Numbers: This is a bit more complex, based on whether you are running on a 32 or 64 bit system. On a 32 bit system, numbers are generally stored as 32 bit integers, and 64 bit systems default to 64 bit integers. You can read more detail on MongoInt32 and MongoInt64.
  • Dates: Known as the MongoDate class, which are based on milliseconds since the epoch.
  • Null values: You can assign NULL values from PHP as well.
Advertisements

One thought on “MongoDB Data Types

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s