Seven NoSQL Databases in a Week
上QQ阅读APP看书,第一时间看更新

MongoDB data types

Documents in MongoDB are JSON-like objects. JSON is a simple representation of data. It supports the following data types:

  • null: The null data type is used to represent the null value as well as a value that does not exist:
  • boolean: The boolean type is used to represent true and false values:
  • number: In MongoDB, the shell default supports 64-bit floating-point numbers. To process long and integer numbers, MongoDB provides NumberLong and NumberInt, which represent 4 bytes and 8 bytes, respectively.
  • string: The string data type represents the collection of characters. The MongoDB default supports UTF-* character encoding:
  • date: MongoDB stores dates in milliseconds since the epoch. The time zone information is not saved:
  • After inserting a date using the preceding way in the document, when we query using find it returns a document with a date in the following format:
  • array: A set or list of values represents arrays. Also, multiple JSON objects represent an array of elements. The following example shows an array of city values:
  • Embedded document: Another MongoDB document-like structure, which can also be used as a key. In the following screenshot, we are storing address fields as an array of addresses, instead of creating a separate collection of addresses:
>