Type of field in collection

Hi can any one help me
I want get all type of value in bson

The bson format specification is here.

They tell you how the bit pattern that represents each data type allowed in BSON.

I add details below that you may find useful or not, and an example with some types.


Basically the format cited above explains how the data you type is serialized (characters in the screen to bytes) and deserialized (bytes to data.)

BSON was created bc:

  • when JSON is serialized the type information is not specific enough to make reading fast. I don’t know the details of this.

  • It also adds flexibility. JSON has a similar serialization but lacks types for binary data and date.

( I am not sure how this is handled with drivers, I presume they send JSON over the network and when the Driver queries a Database deserializes it to json to send it over the network as well.)


Example of a bson doc adapted from here

// this "text" document
{ "BSON" : [ "awesome", 5.05, 1986 ] }
// serializes to the "binary" document
 \x31\x00\x00\x00 
 // total size 49 bytes, 45 left now (int32)
 \x04BSON\x00 
 // 04 is Array, 39 left now
 \x26\x00\x00\x00
 // size 38 for Array? I don't really know what is this
 \x02\x30\x00\x08\x00\x00\x00awesome\x00
  // 02 is utf string (awesome is actually 61 77 65 73 6f 6d 65)
 // 30 is utf 8 for 0 i.e first item in the object / array
// notice the "separator" \x00 right after
 \x01\x31\x00\x33\x33\x33\x33\x33\x33\x14\x40
 // 01 is double,so don't expect me to translate it lol, but can be pasted elsewhere
 // 31 is first item in the array
 // notice the "separator" \x00 right after
 \x10\x32\x00\xc2\x07\x00\x00
  // 32 is the utf-8 key again, 2 in this case
  // notice the "separator" \x00 right after 
  //  (7 << 8) + 0xc2 gets you to 1982
 \x00
1 Like

BSON is used on the network too. The query itself is a BSON document!

1 Like

Makes sense now that you say it. Thank you.