MongoDB (6)-BSON data type


  • BSON is a binary serialization format used to store documents in MongoDB and make remote procedure calls
  • The data structure is similar to JSON, but supports richer data types

type of data

type of dataSerial numberAliasRemarks
Binary data5binData 
Regular Expression11regex 
JavaScript code with scope15javascriptWithScope4.4 Deprecation
32-bit integer16int 
64-bit integer18long 
Min key-1minKey 
Max key127maxKey   

What is the use of the serial number?

When you want to specify a data type through $type, you can use the serial number instead of typing the complete string

As you can see, when specifying the data type through $type, you can use aliases or serial numbers


The ObjectId is very small, may be unique, is generated quickly, and is in order. The length of the ObjectId value is 12 bytes, including

  • A 4-byte timestamp value, representing the creation of the ObjectId, in seconds since the Unix epoch
  • A 5-byte random value
  • A 3-byte incrementing counter, initialized to a random value

The benefits of adding an ObjectId to _id

  • Sorting the _id field that stores the ObjectId value is roughly equivalent to sorting by creation time
  • In the mongo shell, you can use the ObjectId.getTimestamp() method to access the creation time of the ObjectId


  • BSON string is UTF-8
  • When serializing and deserializing BSON, the driver of each programming language converts the string format of that language to UTF-8
  • This makes it easy to store most international characters in BSON strings
  • In addition, MongoDB $regex queries support UTF-8 in regex strings


  • BSON has a special timestamp type for internal MongoDB use, and has nothing to do with the regular Date type
  • This internal timestamp type is a 64-bit value
  • The first 32 bits are the time_t value (depending on the number of seconds up to 1970), and the last 32 bits are the increasing ordinal number of operations in a given second
  • In a single mongod instance, the timestamp value is always unique

new Timestamp()

When inserting a document containing a top-level field with an empty timestamp value, MongoDB will replace the empty timestamp value with the current timestamp value

db.myNewCollection1.insert({test:new timestamp()}) db.myNewCollection1.find(){ "_id" : ObjectId("542c2b97bac0595474108b48"), "ts" : Timestamp(1412180887, 1) }


BSON Date is a 64-bit integer representing the number of milliseconds since the Unix epoch (January 1, 1970)

Construct a date in the shell

var mydate1 = new Date()

Use the ISODate() constructor to construct a Date in the mongo shell

var mydate2 = ISODate() 

Date constructed by two functions, the values ​​are of the same type

Return the date value as a string


Returns the month part of the date value

Month is zero index, so January is month 0


Comparison order of different BSON data types

  1. MinKey (internal type)
  2. Null
  3. Numbers (ints, longs, doubles, decimals)
  4. Symbol, String
  5. Object
  6. Array
  7. BinData
  8. ObjectId
  9. Boolean
  10. Date
  11. Timestamp
  12. Regular Expression
  13. MaxKey (internal type)

I'll talk about it later