MongoDB Learning (ii): Data types and basic concepts

Source: Internet
Author: User

This article address: http://www.cnblogs.com/egger/archive/2013/04/27/3047191.html welcome reprint, please keep this link???????!

Data typeBasic data types

MongoDB's file storage format is Bson, which supports inserting document objects and arrays into other document objects and arrays as well as JSON, extending the JSON data type. Those applications that deal with databases. For example, JSON does not have a date type, which makes it tedious to handle a simple date problem. There is only one numeric type that cannot distinguish between floating-point numbers and integers, but not 32-and 64-bit digits. There is no way to represent other common types, such as regular expressions or functions.

The following are the supported data types for MongoDB:

  • null null is used to represent a null value or a field that does not exist. {"X": null}

  • The boolean Boolean type has two values of ' true ' and ' false1 '. {"X": true}

  • A 32-bit integer type is not available. JavaScript only supports 64-bit floating-point numbers, so 32-bit integers are automatically converted.

  • This type is not supported for 64-bit integers . The shell uses a special inline document to display a 64-bit integer.

  • 64-bit floating point numbers in the shell are this type. The following representations are floating-point numbers: {"x": 3.1415926} {"X": 3}

  • string UTF-8 strings can be represented as string types of data: {"x": "Foobar"}

  • symbols Do not support this type. The shell converts the symbol types in the database into strings.

  • object ID Object ID is the 12-byte unique ID of the document, {"X": ObjectId ()}

  • The date -date type stores the number of milliseconds that start from the standard era. Do not store time zone: {"X": New Date ()}

  • Regular expressions can contain regular expressions, using JavaScript's regular expression syntax: {"x":/foobar/i}

  • The code document can also contain JavaScript code: {"X": function () {/* ...}}

  • Binary data binary data can consist of strings of arbitrary bytes. However, the shell cannot be used.

  • Maximum Value Bson includes a special type that represents the maximum possible value. There is no such type in the shell.

  • Minimum Value Bson includes a special type that represents the minimum possible value. There is no such type in the shell.

  • undefined type can also be used in undefined documents: {"X": undefined}

  • A collection of array values or a list that can represent a group: {"x": ["a", "B", "C"]}

  • An inline document document can contain other documents or can be embedded as values in the parent document, and the data can be organized more naturally without having to be stored in a flat structure: {"x": {"Food": "Noodle"}}

Digital

There is only one type of "number" in JavaScript. Because there are 3 types of numbers in MongoDB (32-bit integers, 64-bit integers, and 64-bit floating-point numbers), the shell must bypass JavaScript restrictions. By default, the numbers in the shell are treated by MongoDB as a double-precision number. This means that if you get a 32-bit integer from the database, after you modify the document, the integer is converted to a floating-point number when the document is stored back into the database, even if the integer remains intact. So it's wise to try not to cover the entire document under the shell.

Date

The JavaScript Date object is used as a type of MongoDB, and when a new Date object is created, it is usually called New Data ("").

Basic concepts

The following will be in accordance with the learning Rdms first introduce similar lines, tables, database concepts of knowledge, and then to learn to add or delete changes and other operations.

1. Documentation

A document is a basic unit of data in MongoDB (similar to a row in a relational database, but much more complex than a row). Multiple keys and their associated values are placed together in an orderly manner as documents. The file storage format for MongoDB is Bson.

BSON is the abbreviation for binary JSON and is a binary encoded format for JSON document objects. Bson supports inserting document objects and arrays into other document objects and arrays as well as JSON, extending the JSON data type. For example: Bson has a date type and a bindate type. Bson has three features: light weight, ergodic, high efficiency

The document can be represented as:
{"FirstName": "Egger", "LastName": "Wong"}

Note the point:

    1. The key/value pairs in the document are ordered.

    2. The value in the document can be a string not only in double quotes, but also in several other data types (even the entire embedded document).

    3. MongoDB distinguishes between type and case.

    4. MongoDB documents cannot have duplicate keys.

    5. The key of the document is a string. In addition to a few exceptions, keys can use any UTF-8 character.

    • The key cannot contain a/s (null character). This character is used to denote the end of a key.

    • . and $ have special meaning and can only be used in certain circumstances.

    • Keys that begin with the underscore "_" are reserved (not strictly required).

2. Collection

A collection is a set of documents (similar to a table in a relational database) that can be viewed as a table without schemas.

No mode

The collection is modeless. This means that the documents within a collection can be of various kinds. For example, the following two documents can exist in the same collection:
{"Name": "Egger"}
{"Age": 18}

The above document is not just a different type of value (a string and an integer), and their keys are not exactly the same.

Although you can place any document inside a collection, it is recommended to use multiple collections:

    • Mixing all kinds of documents in a single collection is a nightmare for both developers and administrators alike.

    • It is also very fast to ask a particular type of document in a collection, and it is much faster to do multiple collections separately.

    • Place documents of the same type in a collection so that the data is more concentrated.

    • When creating an index, the document will have an additional structure (especially when there is a unique index). Indexes are defined according to the collection. Putting documents of the same type into the same set can make the index more efficient.

Named

We can identify the collection by name. The collection name can be any UTF-8 string that meets the following criteria.

    • The collection name cannot be an empty string "".

    • The collection name cannot contain the \ s character (null character), which represents the end of the collection name.

    • The collection name cannot be "system." Begins with a prefix reserved for the system collection.

    • User-created collection names cannot contain reserved characters of $. Some drivers do support the inclusion of $ in the collection name, because some system-generated collections contain that character. Never show $ in a name unless you want to access a collection created by this system.

Sub-collection

One Convention for organizing collections is to use the "." A subset of characters separated by namespace. It's a good way to use subcollections to organize your data in MongoDB

For example, a personal information might contain two collections, namely Person.name and Person.age. This is done only to make the organizational structure better, meaning that the person's collection (where there is no need to exist) has no relation to its subcollections. Put the name of the database in front of the collection name, and get the fully qualified name of the collection, called the namespace . The length of the namespace must not exceed 121 bytes and should be less than 100 bytes in actual use.

Many MongoDB tools contain subcollections.

    • Gridfs is a protocol that stores large files and uses subcollections to store the file's metadata so that it is separate from the content block

    • The MongoDB Web Console organizes the data in the Dbtop section by means of subcollections.

    • Most drivers provide syntax sugar, which facilitates access to subsets of a specified collection.

3. Database

Multiple collections in MongoDB can form a database. A single instance of MongoDB can hold multiple independent databases, each with its own collection and permissions, and different databases placed in different files.

The database is also identified by name. The database name can be any UTF-8 string that meets the following criteria.

    • Cannot be an empty string ("").

    • Must not contain ' (space) 、.、 $,/, \, and (empty).

    • should be all lowercase.

    • Up to 64 bytes.

Some database names are reserved and can be accessed directly from these databases with special effects.

? Admin
From the perspective of permissions, this is the "root" database. If you add a user to this database, the user automatically inherits permissions from all databases. Some specific server-side commands can only be run from this database, such as listing all databases or shutting down the server.
? Local
This data is never copied and can be used to store any collection that is limited to a local single server
? Config
When MONGO is used for sharding settings, the Config database is used internally to hold information about the Shard.


MongoDB Learning (ii): Data types and basic concepts

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.