I. Data Types
1. The document of the basic data type MongoDB is similar to JSON, and the object in Javascript is similar in concept. JSON is a simple data representation method that only contains6 TypesData type.Only null, Boolean, number, String, array, and object types are supported..
MongoDB adds other data types based on its basic JSON key/value pairs.
Null
Null indicates a null value or a nonexistent field {"X": NULL}
Boolean
Boolean has two values: 'true' and 'false' {"X", true}
32-bit integer
This type is not available in shell. As mentioned above,Javascript only supports 64-bit floating point numbers, so 32-bit integers are automatically converted.
64-bit integer
Shell does not support this type. Shell uses a special embedded document to display 64-bit integers.
64-bit floating point number
The numbers in shell are of this type. The following is a floating point {"X": 3.14}. This is also a floating point: {"X": 3}
String
Both UTF-8 strings can be represented as string type data:
{"X": "foobar "}
Symbol
Shell does not support this type. Shell converts the symbol types in the database to strings.
Object ID
The object ID is the unique ID of 12 bytes of the document.
{"X": objectid ()}
Date
The date type stores the number of milliseconds starting from the standard epoch. Do not store the time zone:
{"X": new date ()}
Regular Expression
The document can contain regular expressions, using the regular expression syntax of javascript:
{"X":/foobar/I}
Code
This document also contains JavaScript code: {"X": function (){/*...*/}}
Binary data
Binary data can be composed of any byte string. But the service in shell is used.
Maximum Value
Bson includes a special type, indicating the maximum possible value. Shell does not have this type
Minimum value
Bson includes a special type, indicating the possible minimum value. This type is not found in shell.
Undefined
You can also use the undefined type in the document (the null and undefined types in JavaScript are different)
{"X": Undefined}
Array: a set or list of values can be expressed as an array.
{"X": ["A", "B", "C"]}
Embedded document documents can contain non-dynamic documents, or can be embedded into the parent document as values.
{"X": {"foo": "bar "}}
2. Number
Javascript has only one "Number" type. Because MongoDB has three numeric types (32-bit integer, 64-bit integer, and 64-bit floating point number), Shell must bypass Javascript restrictions.
By default,The numbers in shell are treated as double-precision data by MongoDB..
This means that if you get a 32-bit integer from the database, after modifying the document and saving the document back to the database, the integer is also converted to a floating point number, keep this integer intact in time.
Therefore, it is wise not to overwrite the entire document in shell.
A number can only be expressed as a double-precision number (64-bit floating point number)
3 date
In JavaScript, the date object is used as the date type of MongoDB. When creating a new date object, new date (...) is usually called, not just date (...)
The date display in Shell uses the local time zone settings. However, the date is stored in milliseconds starting from the standard epoch, and there is no related time zone information.
4 Array
An array is a set of values that can be operated as an ordered object (such as a list, stack, or Queue) or as a disordered object (such as a set.
In the following document, the value of the "things" Key is an array.
Arrays can contain elements of different data types.
5 embedded documents
The embedded document uses the entire MongoDB document as a key value in another document.
For example, if you use a document to represent a person and save the address, you can embed the address into the "Add-RESS" document.
{
"Name": "John Doe ",
"Address ":{
"Street": "123 Park Street ",
"City": "anytown ",
"State": "NY"
}
}
In relational databases, the preceding documents are generally split into two rows in the "people" and "Address" tables. In MongoDB, you can directly embed the address document into the personnel document.
6 _ id and objectid
The document stored in MongoDB must have a "_ id" key. The key value can be of any type and is an objectid object by default. Each document in a set has a unique "_ id" value to ensure that each document in the set is uniquely identified.
1 objectid is the default type of "_ id"
Objectid uses a 12-byte storage space. Each byte has two hexadecimal numbers and is a 24-Bit String.
First 4Bytes start from the standard epoch.Timestamp, In seconds,
Timestamp, combined with the next five bytes, provides second-level uniqueness
Because the timestamp is in the front, this means that the objectid will be arranged roughly in the order of insertion.
The four bytes also imply the document creation time. Most drivers publish a method to obtain this information from objectid.
Next3 bytesYesUnique Identifier of the host
To ensure that the objectid generated by multiple concurrent threads on the same machine is unique, the next two bytes are from the process identifier (PID) that generates the objectid)
The first nine bytes ensure that the objectid generated by different processes on different machines in the same second is unique. The last three bytes are an automatically added counter, ensuring that the objectid generated by the same process in the same second is also different.
2 automatically generate _ id
If there is no "_ ID" key during document insertion, the system will automatically create one for you.
This can be done by the MongoDB server, but it is usually done by the driver on the client.
Although objectid is lightweight and easy to generate, it still generates overhead.
The design concept of MongoDB is presented in client generation:If you can transfer data from the server to the driver, try to transfer the data. Handing the transaction over to the client reduces the burden of database expansion.
Generate objectid on the client, and the driver can provide richer APIs.