Getting started with mongodb-3 data types-Basic Data Types

Source: Internet
Author: User
Tags mongodb documentation
Mongodb entry-3 data type-Basic Data Type MongoDB documentation uses BSON (BinaryJSON) to organize data. BSON is similar to JSON and JSON is just a simple way to represent data, it only contains six data types (null, Boolean, number, String, array, and object) and cannot fully meet the needs of complex businesses. Therefore, BSON also provides

Mongodb entry-3 data type-Basic Data Type MongoDB documentation uses BSON (Binary JSON) to organize data. BSON is similar to JSON and JSON is just a simple way to represent data, it only contains six data types (null, Boolean, number, String, array, and object) and cannot fully meet the needs of complex businesses. Therefore, BSON also provides

Getting started with mongodb-3 data types-Basic Data Types

MongoDB documents use BSON (Binary JSON) to organize data. BSON is similar to JSON. JSON is just a simple way to represent data, it only contains six data types (null, Boolean, number, String, array, and object) and cannot fully meet the needs of complex businesses. Therefore, BSON also provides date, 32-bit number, 64-bit number, and other types. The following describes the mongoDB types:

1. null indicates a null value or a field that does not exist, for example, {"one": null}

2. The boolean type has two upper values: 'true' and 'false', for example, {"one": true}

3. 32-bit integers are automatically escaped because the mongoDB console uses the JS engine for input and JS only supports 64-bit floating-point numbers;

4. 64-bit integer

The 64-bit integer is the same as the 32-bit integer. When used on the MongoDB console, it is converted to a 64-bit floating point number. Except, if the data type stored by the database itself is a 32-bit integer or a 64-bit integer, you can use the MongoDB console to retrieve the data and change its document records (even if the integer itself is not modified, only the other part of the document is modified), and the data type is changed to 64-bit floating point.

However, when you use the console to view a 64-bit integer, it may be incorrect because some 64-bit integers cannot be exactly represented as 64-bit floating-point numbers, while the console displays all 64-bit floating-point numbers.

5. The default number type of the 64-bit floating point MongoDB console, for example, {"one": 2.02} {"one": 10}

6. String: {"one": "Hello World "}

7. the symbol is not supported in the MongoDB console and is automatically converted to a string;

8. the Object id is the unique 12-bit id in the document,

When MongoDB is used to store documents, there must be a "_ id" key, which can be of any type. If this _ id key is not available when a document is added, the system will automatically generate a value using the ObjectId object. In a distributed environment, different machines can generate values using the globally unique method of the same type. The generation rules are as follows:

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

Timestamp | machine | PID | counter

The first four digits indicate the timestamp. The timestamp is in seconds. Because the timestamp is earlier, it can better reflect the time sequence during data insertion, making the data easier to query, it is recommended that the index be easier.

Although the system automatically creates the _ id key, we recommend that you use the client driver to create the key for highly concurrent applications. The main reason is that although ObjectId can be generated, it will still generate overhead and increase the burden on the database.

In a highly concurrent distributed environment, only the timestamp in seconds is used and the machine cannot distinguish its uniqueness. Therefore, a PID is added after it, that is, the MongoDB process identifier, the first nine characters ensure that the ObjectId generated by different processes on different machines in the same second is unique, and the two are an auto-incrementing counter, so that the ObjectId generated by the same process in the same second is different.

9 The number of milliseconds since the standard epoch {"date": new Date ()}

10 The regular expression document can contain regular expressions, which follow the JavaScript syntax {"foo":/foobar/I}

11 The code document may contain JavaScript code {"x": function (){}}

12 binary data is composed of any byte binary string, which is not supported by shell.

13. The maximum value indicates the maximum possible value, which is not supported by shell.

14 The minimum value indicates the possible minimum value, which is not supported by shell.

15 undefined {"x": undefined}

16. set or list of array values {"arr": ["a", "B"]}

17 The embedded document can serve as the value of a key in the document {"x": {"foo": "bar "}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.