Various data types in MongoDB

Various data types in MongoDB

MongoDB supports many data types which includes

  • Object – It is used for embedded documents.
  • Null – It stores a ‘null’ value.
  • String – String must be UTF-8 for being valid.
  • Number – MongoDB defaults to using 64-bit floating point numbers and for integers, the NumberInt or NumberLong is used which are 4-byte or 8-byte signed integers.
  • Boolean – It stores a boolean (true/ false) value.
  • Double – It stores floating point values.
  • Min/Max keys – It compares a value against the lowest and highest BSON elements.
  • Arrays – It can store sets or list or multiple values into one key. Arrays can contain different data types as values even nested arrays. MongoDB can query on arrays and build indexes using their contents with atomic updates.
  • Timestamp (ctimestamp) – It is usually used to record modify or add time of a document.
  • Symbol – It is similar to a string but, reserved for languages needing a specific symbol type.
  • Date – It stores the current date or time in UNIX time format and are stored as milliseconds since the epoch though displayed as per local time zone setting. User can also specify own date time by creating object of Date class by calling new Date(…) constructor method which returns a string representation of the date and passing day, month, year into it.
  • Binary data – It stores binay data usually to store non-UTF-8 strings.
  • Code – It stores Javascript code into document.
  • Regular expression – It stores regular expression or regex using JavaScript’s regular expression syntax.
  • Embedded Document – Documents can contain whole of other documents embedded as values in a parent document. MonoDB can build indexes, query or make updates in them.
  • Code – Queries and documents can also contain arbitrary JavaScript code.
  • Object ID – It stores the document’s ID and gives globally unique identity across different machines. Every document in MongoDB must have a “_id” key whose value can be any type, but defaults to an ObjectId. It needs 12 bytes of storage or 24 hexadecimal digits with the first four bytes stores timestamp in seconds since epoch, the next three bytes store machine identifier usually a hash of the machine’s hostname, the next two bytes stores PID or process ID and remaining stores increment value. The “_id” key is added to newly inserted document if not present and is generally done by the driver on the client side.
Share this post
[social_warfare]
Documents, Collections and Shell
Product Attributes

Get industry recognized certification – Contact us

keyboard_arrow_up