Home > Articles > Data

  • Print
  • + Share This
This chapter is from the book

MongoDB Data Types

The BSON data format provides several different types used when storing the JavaScript objects to binary form. These types match the JavaScript type as closely as possible. It is important to understand these types because you can actually query MongoDB to find objects that have a specific property with a value of a certain type. For example, you can look for documents in a database whose timestamp value is a String object or query for ones whose timestamp is a Date object.

MongoDB assigns each data type of an integer ID number from 1 to 255 when querying by type. Table 1.1 lists the data types MongoDB supports, along with the number MongoDB uses to identify them.

Another point to be aware of when working with the different data types in MongoDB is the order in which they are compared when querying to find and update data. When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest:

  1. Min key (internal type)
  2. Null
  3. Numbers (32-bit integer, 64-bit integer, double)
  4. Symbol, String
  5. Object
  6. Array
  7. Binary data
  8. Object ID
  9. Boolean
  10. Date, timestamp
  11. Regular expression
  12. Max key (internal type)
  • + Share This
  • 🔖 Save To Your Account