MongoDB Getting Started tutorial three [data type]

Source: Internet
Author: User
Tags local time string format mongo shell

MongoDB Documents use Bson (Binary JSON) to organize data, bson similar to Json,json is simply a way of representing data, containing only 6 data types (NULL, Boolean, number, string, array, and object). Does not fully meet the needs of complex business, therefore, Bson also provides a date, 32-digit number, 64-digit number and other types. The following is a brief description of the type of MongoDB:1, null[type number: ten]A null type is used to represent a null or nonexistent field, such as: {"x": null}2. Boolean type boolean[type number: 8]The Boolean type has two values, ' true ' and ' false ', such as: {"X": True}3, 32-bit integer 32-bit integer[type number: +]The 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 64-bit integer[type number: +]A 64-bit integer, like a 32-bit integer, is escaped into a 64-bit floating-point number when used in the MongoDB console. Except, if the data type stored by the database itself, whether it is a 32-bit integer or a 64-bit integer, is obtained by using the MongoDB console, changing its document record (even if the integer itself is not modified, only the other parts of the document are modified), and the console is reused to write back to the database. The data type also becomes a 64-bit floating-point number. Except that when you use the console to view a 64-bit integer, you may be unsure because some 64-bit integers cannot be accurately represented as 64-bit floating-point numbers, and console rendering is 64-bit floating-point numbers.5. Numeric type: double[type number: 1]

In the MONGO shell, 64-bit floating-point data is used by default. Therefore, there are two numerical forms:

{"X": 2.32}//or {"X": 2}

For integer types, you can use either the Numberint () (bit signed integer) or the Numberlong () (8-bit signed integer) method to convert. Examples are as follows:

{"X": Numberint (2)} {"X": Numberlong (2)}
6. String type: string[type number: 2]The string type in MongoDB is represented using UTF-8 encoded characters. For example: {"x": "Hello Quickcodes"}7. Sign symbol [type number: +]This type is not supported in the MongoDB console and will be automatically escaped into a string;8. Object ID id[Type number: 7]The object ID is the only 12-bit ID in the document, which must have a "_id" key in MongoDB to store the document, which can be any type, and if there is no _id key when the document is added, the system will automatically generate one using the Objectid object, in a distributed environment, Different machines can generate values in a globally unique, homogeneous way, such as: {"_id": ObjectId ("57110709d1db0802a433a03c")}
Its generation rules are:0 | 1 | 2 | 3 |4 | 5 | 6 |7 | 8 |9 |Time Stamp |   Machine |    PID | The first 4 bits of the counter represent timestamps, timestamps in seconds, and because of the timestamp in front, it is better to reflect the time order of data insertion, make the data easier to query, and recommend indexing easier. Although the _ID key is created automatically, it is recommended to use the client driver for high-concurrency applications, mainly because, although objectid can be generated, the system generates overhead and increases the burden on the database when it is built. In a highly concurrent distributed environment, only using timestamps in seconds and machines cannot differentiate their uniqueness, a PID is added behind it, the process identifier of MongoDB, and the first 9 characters guarantee the Objectid of different processes of different machines in the same second. The two-bit is an auto-incrementing counter that ensures that the same process is not the same as the objectid produced in the same second.

Related functions: ObjectId (), Gettimestamp (), ValueOf ()

9th Date [type number: 9]The date in MongoDB is represented by a timestamp, in milliseconds, and no time zone is stored. The number of milliseconds starting from the standard ERA {"Date": New Date ()} When you create a Date object, you should use the new date () instead of the constructor date (). The date format returned when the constructor is used as a function is a string, not a Date object (related to the JavaScript working mechanism).
    • In MongoDB, the date type is a 64-bit integer that represents the number of milliseconds from the Unix epoch
    • MongoDB converts to UTC time when storing time

Beijing time (CST) = UTC + 8 hours

    • The MongoDB shell can use the new date or isodate to create the time object, and when displayed, the shell sets the display date object according to the local time.
10 Regular Expressions Regular expression[type number: one-by-one]A document can contain regular expressions that follow the syntax of JavaScript. Used primarily for queries, using regular expressions as a qualification

For example: {name:/foo/} The Name field contains a document for Foo

{name:/foo/i} Name field contains Foo's document, and is case insensitive

{name:/^foo/i} Name field starts with Foo and is case-insensitive

11 Code JavaScript (Scope) [type number: +]The documentation and code for MongoDB can include JavaScript code. Such as:
{"X": function () {/* Here is a piece of JavaScript code */}}
122 binary Data binaries data[type number: 5]You can save a string of any byte, such as pictures, videos, and so on. A binary string of arbitrary bytes, which the shell does not support. Binary data is a binary byte word string, to save non-UTF-8 characters to the database, only use decimal data.13 Max Max Key [type number: 127]Represents the maximum possible value, the shell does not support14 min min key[type number: 255]Represents the smallest possible value, the shell does not support 15 undefined undefined{"x": undefined}16 Array array[type number: 4]Datasets can be stored in an array format, the same as an array representation in Javasript. Such as:
{"X": ["Cloudev.top", "Quickcodes.net"]}
Arrays can contain different types of data elements, including inline documents and arrays. All MongoDB middle Key-value pairs support data types that can be used as values for arrays.
    • An array is a set of values, expressed in square brackets, that can be used as an ordered object (list, stack, queue), or as an unordered object (such as a collection) to manipulate
    • An array can contain elements of different data types (strings, floats, documents, and so on), for example: [3.14, "Hello", [+/-a], {"Key": "MongoDB"}]
    • Many specific operators are provided for the array MongoDB, such as: $push, $pop, $pull, $slice, $addToSet, etc.
    • MongoDB can automatically establish an Multikey index for an array element
17 An inline document document can be used as the value of a key in a document. The total size of the MongoDB document is limited to 16MB, it is recommended to use the form of sub-documents to organize the data, sub-document query efficiency is higher than the multi-key query.

{

"_id": ObjectId ("57110709d1db0802a433a03c"),

Title: "Hello quickcodes",

Author: "Mac.manon",

comments:[

{nickname: "Peter", Comment: "OK"},

{nickname: "Tom", Comment: "Dig"}]

}

The document can be used as the value of the key, namely: inline document. MongoDB's biggest advantage over relational databases is the embedded documentation. In contrast to the flattened data structure of a relational database, it is more natural to use inline documents to organize the data.

18. Timestamp Timestamp [type number:]

The timestamp type consists of two parts: the Bit-unix epoch and the bit-increment ordinal (same second)

TIMESTMP is only used internally by the MongoDB database service to record the detailed time of the operation;

The Timestamp type is not related to the date type, and more date types are used for us;

Correlation function: Timestamp ()

All of the above data types have a type number, such as a string type number of 2, the following instruction in the user table looks up the field name as name, and the data is saved in string format:

Db.user.find ({name:{$type: 2}})

MongoDB Getting Started tutorial three [data type]

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.