mongodb data type of null and string and number and date and embedded document and array etc

  • 2020-06-12 10:55:14
  • OfStack

MongoDB's documentation is similar to JSON in that JSON is just a simple way to represent data and contains only six data types (null, Boolean, number, string, array, and object).

Limitations of JSON's data types:
1. There is no date type, and the processing of date type is relatively complicated
2. Unable to distinguish between floating point and integer, 32-bit and 64-bit
3. Other types represent limitations such as functions, regular expressions, etc

Mongodb USES BSON (Binary JSON) to organize data, and BSON also provides date, 32-bit, 64-bit, and other types. Here's how these types are represented in the documentation in mongodb shell:

1. null is used to represent null values or fields that do not exist.

{"name":null}

2. Boolean type, true and false

{"flag":true}/{"flag":false}

3. The number mongodb is divided into 32-bit integer, 64-bit integer and 64-bit floating point number

> JavaScript only supports 64-bit floating-point Numbers, and 32-bit integers in shell are automatically converted:
By default, the number in shell is treated as double by mongdb. If you take a 32-bit integer from the database, the modified document is converted to a floating point number when it is saved to the database.
> In shell, 64-bit integers will be displayed as embedded documents:
Another problem with representing digital intelligence as a double (64-bit floating-point number) is that some 64-bit integers do not represent 64-bit floating-point Numbers accurately. So if you store a 64-bit integer and look it up in shell, it displays a nested document that may not be accurate.
Such as:
Insert document {"name":"apple","num":5}, where "num" is set to a 64-bit integer 5.
See below:
{ "_id" : ObjectId("5000f7af32e3988ad237a202"), "name" : "apple", "num" : {"floatApprox" : 5} }

Insert document {"name":"orange","num":9223372036854775807}
See below:
{" _id ": ObjectId (" 5000 f7af32e3988ad237a203"), "name" : "apple", "num" : {" floatApprox: "9223372036854776000," top ": 2147483647," bottom ": 4294967295}}

For the above examples (try to witness them yourself), see the Mongodb Authoritative Guide

I inserted the following document on the machine (win7 32-bit) :
{"name":"apple","num":5}
{"name":"orange","num":9223372036854775807}
{"name":"peach","num":922337203685477580743254354565678678998935345}

> db.a.find()
{ "_id" : ObjectId("5000f7af32e3988ad237a204"), "name" : "apple", "num" : 5 }
{ "_id" : ObjectId("5000f87432e3988ad237a205"), "name" : "orange", "num" : 9223372036854776000 }
{ "_id" : ObjectId("5000fbd232e3988ad237a206"), "name" : "peach", "num" : 9.223372036854776e+44 }


64-bit floating point:
{"num":3.14}/{"num":3}

UTF-8 strings can be represented as string type data

{"name":"orange"}

5. The symbol shell is not supported and converts all symbol types in the database to strings

6. Object id is the only 1ID of the 12-byte document

{"x":Object()}
Documents stored in Mongodb (in the same collection) must have only one "_id" key. Values can be of any type, must be unique, and default to ObjectId objects. As to why mongodb USES ObjectId objects, and what benefits it has, please refer to relevant materials.

The following is the composition of ObjectId:
ObjectId USES 12 bytes of storage space, two decimal digits per byte, and is a 24-bit string. The 12 bytes are generated as follows:

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
Time stamp | machine code | PID | counter
-----------------------------------------------

> Timestamp: A timestamp in seconds starting from a standard era.
> Machine code: The only 1 identifier on the host, usually the hash value of the machine's host name.
> PID: The process identifier that generates ObjectId.
> Counter: Automatically increments counter, same as allowing up to 256 to the third power (16777216) ObjectId per second.

Document's "_id" key:
1. When the document is inserted, the system will automatically create one without this key.
2.mongodb server can be created automatically, usually by the driver at the client:
> Reduce the burden of database expansion
> The driver provides rich API, can return ObjectId, also can insert document; If there is server generation, the driver needs a separate query to determine the "_id" value in the inserted document.

7. Date-type stores the number of milliseconds since the standard era, not the time zone.

{"name":"xx","date":new ()} {" _id ": ObjectId (" 5001047632 e3988ad237a206"), "name" : "xx", "date" : ISODate (" the 2012-07-14 T05: place. 189 Z ")}

8. Regular documents can contain regular expressions, using javascript's regular expression syntax

{"regex":/[0-9]/g}

9. Code or javascript code (the value in mongodb is saved directly to the database and is not executed, so there is no security problem)

{"setName":function(){var name="jk";alert(name)}}

10.2 Decimal Numbers can consist of any string of bytes. Not available in shell.

BSON includes a special type that represents the maximum possible. shell does not have this type.

12. Minimum BSON includes a special type that represents the possible minimum. shell does not have this type.

13. Undefined types can also be used in undefined (undefined) documents

{"name":undefined}

14. A collection or list of array values can be represented as an array

{"name":["s","d","f"]}

A document can contain a document, embedded as a value in the parent document

{"sch" : "s"
"catl" : {
"c1" : {"cls1" : "cs1","cls2" : "cs2","cls3" : "cs3"}
"c2" : {"cls1" : "cs1","cls2" : "cs2","cls3" : "cs3"}
"c3" : {"cls1" : "cs1","cls2" : "cs2","cls3" : "cs3"}
}
}

From an object-oriented perspective, a document is an object, and the key/value in the document are attributes and attribute values.


Related articles: