Json
JSON is a simple way to represent data, it is easy to understand, easy to parse, easy to remember. But on the other hand, JSON has some limitations because there are only a few types of data such as NULL, Boolean, number, string, array, and object. For example, JSON has no date type, JSON has only one numeric type, cannot distinguish between floating-point numbers and integers, let alone 32 and 64-bit numbers. Furthermore, JSON cannot represent other common types, such as regular expressions or functions.
BSON
BSON (binary serialized Document format) is a binary form of a class JSON storage format, referred to as binary JSON. Like JSON, it supports inline document objects and array objects, but Bson has some data types that JSON does not have, such as date and bindata types. It supports the following data types. Each data type corresponds to a number, and in MongoDB you can use the $type operator to view the Bson type of the corresponding document.
Type |
corresponding numbers |
Alias |
Description |
Double1 |
1 |
Double |
|
String |
2 |
String |
|
Object |
3 |
Object |
|
Array |
4 |
Array |
|
Binary data |
5 |
Bindata |
|
Undefined |
6 |
Undefined |
Deprecated |
ObjectId |
7 |
ObjectId |
|
Boolean |
8 |
"BOOL" |
|
Date |
9 |
"Date" |
|
Null |
10 |
"NULL" |
|
Regular Expression |
11 |
"Regex" |
|
Dbpointer |
12 |
"Dbpointer" |
|
Javascript |
13 |
"JavaScript" |
|
Symbol |
14 |
"Symbol" |
|
JavaScript (with scope) |
15 |
"Javascriptwithscope" |
|
32-bit integer |
16 |
"Int" |
|
Timestamp |
17 |
"Timestamp" |
|
64-bit integer |
18 |
"Long" |
|
Min Key |
-1 |
"Minkey" |
|
Max Key |
127 |
"Maxkey" |
|
The relationship between BSON and MongoDB
Bson is a document format used to store MongoDB "documents". The driver is used when inserting, querying, or other operations with the document. The document is encoded in Bson format and then sent to the server. Similarly, when the server returns the document to the client, it is also in Bson format. The driver first decodes this bson and then passes it on to the client. Therefore, the relationship between Bson and MongoDB is: MongoDB uses Bson format to store data and transmit data.
Basic data types
null: Used to represent a control or a nonexistent field, such as: {"x": null}.
Boolean : Only two values are true and false.
32-bit integer : This type is not available in the shell, JavaScript only supports 64-bit floating-point numbers, so 32-bit integers are automatically converted.
64-bit integer : This type is not available in the shell, and the shell uses a special inline document to display a 64-bit integer.
64-bit floating-point numbers: The number in the shell is this type, {"X": 3.14} and {"X": 3} are all floating points.
Since JavaScript has only one numeric type that is 64-bit floating point, the numbers from the shell in MongoDB are treated as 64-bit floating-point types, while MongoDB supports three number type, so the shell modifies the data in the database to be converted to 64-bit floating-point. A 64-bit integer does not accurately represent a 64-bit floating point, and if a 64-bit integer is stored in MongoDB, when viewed in the shell, if it can represent a 64-bit floating-point type then the built-in document with a key is displayed and the value is accurate, otherwise, he displays a multi-key embedded document that may be inaccurate.
If it is a 64-bit integer 3, then the query display in the shell would look like this:
Db.nums.findOne () { "_id" : ObjectId ("4c0beecfd096a2580fe6fa08" ) ), "myinteger" : { "floatapprox " 3 } }
If it is a 64-bit integer 9223372036854775807, then the query display in the shell would look like this:
Db.nums.findOne () {"_id": ObjectId ("4c0beecfd096a2580fe6fa08"), "Myinteger" : { "Floatapprox":9223372036854775807, "Top":2147483647, "Bottom":4294967295 } }
Top and bottom represent high 32-bit and low 32-bit respectively.
Note: There is only one type of "number" in JavaScript. Because there are 3 types of numbers in MongoDB (32-bit integers, 64-bit integers, and 64-bit floating-point numbers), the shell must bypass JavaScript restrictions. By default, the numbers in the shell are treated by MongoDB as a double-precision number. This means that if you get a 32-bit integer from the database, after you modify the document, the integer is converted to a floating-point number when the document is stored back into the database, even if the integer remains intact. So it's wise to try not to cover the entire document under the shell.
string : The UTF-8 string can be represented as a string type of data, such as: {"name": "Mary"}.
symbol : The shell does not support this type, and the shell converts the symbol type in the database into a string.
Object ID: The object ID is a unique 12-byte ID for the document.
The document stored in MongoDB must have a key "_id", this key can be any type, the default is the Objectid object, when we save the document without specifying the key, then MongoDB will automatically add such a key value pair, this value is a unique identifier, Objectid uses 12 bytes of storage space.
Date : The date type stores the number of milliseconds starting from the standard era and does not store the time zone, such as {"X": New Date ()}.
In JavaScript, the Date object is used as the type of the MongoDB, and the creation date object is returned with the new date () instead of date (), and the string representation of the date, not the true Date object.
Regular Expressions : The document can contain regular expressions, using JavaScript's regular expression syntax, such as: {"X":/foobar/i}.
Code : The document can also contain JavaScript code, such as: {"X": function () {/*...*/}}.
Binary data : Binary data can consist of strings of any byte, but cannot be used in the shell.
Maximum value : Bson includes a special type that represents the maximum possible value, which does not have this type in the shell.
Minimum value : Bson includes a special type that represents the minimum possible value, which is not in the shell.
undefined: An undefined type can also be used in a document, such as: {"X": undefined}
Array : A collection of values or a list that can represent an array of elements that can be of different types of data, such as {"X": ["a", "B", "C", 20]}.
Inline Document : A document can contain other documents, or it can be embedded as a value in a parent document, such as {"X": {"foo": "Bar"}}.
Object ID: The object ID is a 12-byte string, which is a unique identifier for the document, {"X": ObjectId ()}.
Binary data: binary data is a string of any byte. It cannot be used directly in the shell. If you want to save non-utf-characters to the database, the binary data is the only way.
code: queries and documents can include any JavaScript code, {"X": function () {/*...*/}}
Use the case: Boolean, Number, string, date, array and inline documents are the most used.
Data type of MongoDB (iv)