MongoDB learning notes three basic types

Source: Internet
Author: User
Tags time in milliseconds

MongoDBDocument usageBson(Binary JSON) To organize data,BsonSimilarJSON,JSONIt's just a simple way to represent data. It only contains6Data Types (Null, Boolean, number, String, array, and object), cannot fully meet the needs of complex business, therefore,BsonDate,32Digit,64Digit type. The followingMongoDBType:

 

1,NullNullType indicates a null value or a field that does not exist, for example:{"One": NULL}

2,Boolean Type The boolean type has two upper values,'True'And'False'Such:{"One": true}

3,32Bitwise integerBecauseMongoDBConsole usageJSEngine, andJSOnly supported64Bit floating point number, so32The bitwise integers are automatically escaped;

4,64Bitwise integer 

64Bitwise integers and32The bitwise integers are the same.MongoDBWhen using the console, it is converted64Bitwise floating point number. Except if the data type stored by the database itself is32Integer or64A bitwise integer.MongoDBAfter the console obtains the information, it changes its document record (even if the integer itself is not modified, only other parts of the document are modified), and uses the console to write it back to the database, the Data Type will change64Bitwise floating point number.

 

In the console64It may be incorrect because64The integer of BITs cannot be exactly expressed64Bitwise floating point number, and the control table display is64Bitwise floating point number.

 

5,64Bitwise floating point numberMongoDBThe default type of the console number, for example:{"One": 2.02} {"One": 10}

6,String For example:{"One": "Hello World "}

7,Symbol InMongoDBThis type is not supported in the console and is automatically converted to a string;

8,ObjectID ObjectIDIs unique in the document12BitID,

 

InMongoDBTo store documents, there must be a"_ Id"Key. This key can be of any type._ IdKey, the system will useObjectidAn object is automatically generated. In a distributed environment, different machines can use the globally unique method to generate values.:

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

Timestamp|Machine| PID |Counter

 

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

 

Although the system automatically creates_ IdKey, but inHigh concurrencyWe recommend that you use the client driver for applications.ProgramThe main reason is that althoughObjectidCan be generated, but the system will still produce overhead during production, increasing 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.PID, That isMongoDBProcess identifier, before9Characters to ensure that different processes on different machines generateObjectidIs unique, and the two are an auto-incrementing counter, ensuring that the same process is generated in the same secondObjectidDifferent.

 

9, date the date type is from the standard epoch ( ad 1 year ) the start time in milliseconds. the time zone is not stored, for example, {" one ": new date ()} Note: If you only use date () [none New ], the JS self-contained time type, including the time zone, if different time values are used in documents with the same structure, data management may be inconsistent.

10,Regular ExpressionThe document key value can contain regular expressions, which useJSSyntax, for example:{"One":/ho/I}

11,Code This document may containJSCode For example:{"One": function (){}}

12,Binary data InMongoDBThe console cannot display

13,Maximum Value

14,Minimum value

15,ArrayThe key value in the document can be expressed as an array, but the array does not strictly control the type of members in the data. The types of members in the array can be completely different, and the array can also be nested;

 

16,Embedded document An embedded document uses another document as the value of a key in this document. This seems to be a more reasonable reflection of the relationship between data, for example, we want to express a blogArticleAnd its authors. In relational databases, we generally need to create two tables, one representing blog posts.(Article)The other is the author of the blog.(Author)And then establish a foreign key relationship to controlMongoDBIt can also be expressed in this way

{"_ ID": objectid ("4e75d586f2723d6f1d886771"), "title": "blog test", "Article": {"name": wangxiao, "fullname ": "wangxiaolin"}, "content": "BLOS test "}

 

We can also design it as follows:

{"_ ID": objectid ("comment"), "title": "blog test", "Article": "objectid (" 6e75c586f2723d6f1d886791 ")", "content ": "BLOS test "}

 

{"_ ID": objectid ("6e75c586f2723d6f1d886791"), "name": wangxiao, "fullname": "wangxiaolin "}

 

It is divided into two documents. The better thing is to divide it into two sets, one is the article(Article), One is the author(Author)

 

Although the addition of sub-documents will better reflect the relationship between data, it is easier to query the information associated with data during query, but it will cause data redundancy, which is not conducive to data management. Of course, different design methods must be determined based on the Use scenario.

Related Article

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.