MongoDB (6)-BSON data type

BSON

  • 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
Double1double 
String2string 
Object3object 
Array4array 
Binary data5binData 
Undefined6undefinedDeprecated
ObjectId7objectId 
Boolean8bool 
Date9date 
Null10null 
Regular Expression11regex 
DBPointer12dbPointerDeprecated
JavaScript13javascript 
Symbol14symbolDeprecated
JavaScript code with scope15javascriptWithScope4.4 Deprecation
32-bit integer16int 
Timestamp17timestamp 
64-bit integer18long 
Decimal12819decimal 
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

ObjectId

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
ObjectId("60b441609c297cd9d2ecf60f").getTimestamp()ISODate("2021-05-31T01:52:32Z")

String

  • 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

Timestamps

  • 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) }

Date

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

mydate1.toString()

Returns the month part of the date value

Month is zero index, so January is month 0

mydate1.getMonth()

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