Mongodb Study Notes 2 mongodb getting started (databases, documents, and collections)

Source: Internet
Author: User
Basic concepts of MongOdb: 1. Documents are the basic units of Mongodb data and are similar to rows in relational databases. (But more complex than rows) 2. The set shows that the table has no field attribute. 3. a single real column of Mongodb can contain multiple independent databases, each of which has its own set and permissions. 4. Mongodb is simple but powerful.

Basic concepts of MongOdb: 1. Documents are the basic units of Mongodb data and are similar to rows in relational databases. (But more complex than rows) 2. The set shows that the table has no field attribute. 3. a single real column of Mongodb can contain multiple independent databases, each of which has its own set and permissions. 4. Mongodb is simple but powerful.

Basic concepts of MongOdb:

1. Documents are the basic units of Mongodb data, similar to rows in relational databases. (But more complex than rows)

2. The set shows that the table has no field attribute.

3. a single real column of Mongodb can contain multiple independent databases, each of which has its own set and permissions.

4. Mongodb comes with the concise but powerful mongorcipshell. This tool is very useful for managing Mongodb real-column and operating data.

5. Each document has a special key "_ id", which is unique in the set of documents.


Document
A document is the core concept of MongoDB. Multiple keys and their associated values are put together in an orderly manner.

Most languages have a desired data structure, such as ing, hash, or dictionary. In javascript, the document is represented as an object:

{"Greeting": "Hello world", "age": 30}

This document has only one key "greeting", and its corresponding value is "Hello world". In the case of a large number, the document will be much more complex than this,

Multiple key-value pairs are often included:

{"Greeting": "Hello world", "Hello": "Refactor "}

Key-value pairs in the document are ordered. The document above is different from the document below:

{"Hello": "Refactor", "greeting": "Hello world "}

The value in this document can be a string or other data types. For example, the value of "age" in this example is an integer.

The key in the document is a string. Except for a few exceptions, the key can be any UTF-8 character.

The key cannot contain \ 0 (null), which indicates the end of the key.

. And $ can only be used in a specific environment. If not used properly, the driver will prompt

Keys starting with an underscore "_" are retained, although this is not strictly required.


MongoDB is both case sensitive and case sensitive. The following two documents are different:

{"Age": "30 "}

{"Age": 30}

The following documents are also different:

{"Age": 30}

{"Age": 30}

MongoDB documents cannot have duplicate keys

{"Hello": "Hello world", "Hello": "Refactor"} This is incorrect.


If we use json, we have half of bson's knowledge. I will introduce the newly added data types later.

Example:

{Site: "w3cschool. cc "}

Generally, the term "object" refers to a file.

A file is similar to an RDBMS record.


We can insert, update, and delete collections.

The following table helps you better understand some Mongo concepts:

RDBMS MongoDB
Table) Collection)
Column) Key)
Value) Value)
Records/Rows (Records/columns) Document/Object (Document/Object)

The following table lists several common data types in MongoDB.

Data Type Description
String (string) It can be an empty string or a combination of characters.
Integer) Integer.
Boolean (boolean) The logical value is True or False.
Double Dual-precision floating point
Null It is neither 0 nor empty.
Array Array: a series of values
Object Object type, the entity used in the program. It can be a value, variable, function, or data structure.
Timestamp Timestamp is stored as a 64-bit value. It is unique when only one mongod is run. The first 32 bits are saved in UTC time, in seconds, and the last 32 bits are the Count values in this second. Starting from 0, each time you create a new MongoTimestamp object, one is added.
Internationalized Strings UTF-8 string.
Object IDs Documents in mongodb must be identified by a unique keyword _ id. Almost every mongodb document uses the _ id field as the first attribute (with some exceptions in the system set and Capacity Set (capped collection ). The _ id value can be of any type. The most common practice is to use the ObjectId type.

A collection is a set of documents. If a document is equivalent to a row in a relational database, a collection is equivalent to a table.


The set is stateless, which means that the documents in a set can be various. The following two documents can exist in the same set:

{"Hello": "Refactor "}

{"Age": 30}

Note that the preceding document not only has different types of values (strings and integers), but also has different keys, because any document can be placed in the set,

So there is a question: is it necessary to use multiple sets? If there is no need to divide various document models, why should we use multiple combinations?

The reasons are as follows:

1. Mix all kinds of documents in a set. The developer must either ensure that only the required document types are returned for each query or make the query

Applications are used to process all different types of documents, such as querying blog articles and removing documents containing author data.

2. It is not cost-effective to query specific types of documents in a set. It is much faster to separate multiple sets. For example, a set contains a key of the annotation type.

To query a document whose value is "Refactor1", "Refactor2", or "Refactor3", the query will be slow. If the document is divided into three sets by name, the query will

Fast (see "sub-set ")

3. Put documents of the same type in a collection, so that the data is very concentrated. querying several articles from a collection containing only blog articles will be better

Check several articles in the collection of author data to reduce the consumption of disk seek operations.

4. When creating an index, the document will have an additional structure (especially when there is a unique index). The index is defined according to the set.

Put it in the same set to make the index more effective.


Valid Set Name

The Set Name must start with a letter or underscore.

Set names can protect numbers

The Set Name cannot contain the dollar sign "$", and "$" is a reserved character.

The Set Name cannot exceed 128 characters.

In addition ,". "Number is allowed to be used in the set. They are used as subcollections. For example, if you have a blog set, you can use a blog. title, blog. content or blog. author helps you better organize collections.

Example:

Db. tutorials. php. findOne ()


Capped collections

Capped collections is a collection of fixed sizes.

It has high performance and queue expiration features (expiration is in the order of insertion). It is somewhat similar to the concept of "RRD.

Capped collections is a high-performance automatic maintenance object insertion sequence. It is very suitable for different functions similar to logging and standard collection. You must create a capped collection explicitly and specify the size of a collection, in bytes. The data storage space value of the collection is allocated in advance.

Note that the specified storage size contains the database header information.

> Db. createCollection ("mycoll", {capped: true, size: 100000 })

  • In capped collection, you can add new objects.
  • Objects can be updated. However, objects do not increase storage space. If it is added, the update will fail.
  • The database cannot be deleted. Use the drop () method to delete all rows of the collection.
  • Note: after deletion, you must explicitly recreate the collection.
  • On a 32bit machine, the maximum capped collection size is 1 E9 (1X109) bytes.

Child set

A convention for organizing a set is to separate sub-sets by namespace using the "." character. For example, an application with the blog function may contain two sets,

Blog. posts and blog. authors respectively. This aims to make the organizational structure better, that is, the set of blogs (which may not exist at all)

And Its Subsets have no relationship.


Many MongoDB tools include child sets.

1. GridFS is a protocol for storing large files. It uses sub-sets to store file metadata, which is separated from content blocks.

2. MongoDB web Console organizes data in the DBTOP section by means of sub-sets

3. In the database shell, db. blog represents the blog set, and db. blog. posts represents the blog. posts set.

Using Child sets in MongoDB is the best way to organize data.


Multiple documents in apsaradb for MongoDB form a set, and multiple collections form a database. A MongoDB instance can have multiple databases,

Each database has independent permission control. Different databases are stored on disks.

Store all data of an application in the same database in different files.


Like a set, a database is identified by a name. The database name must meet the following UTF-8 characters:

1. cannot be a null string ("")

2. It cannot contain ''(Space),., $,/, \, and \ 0 (null character)

3. All lowercase letters are required.

4. up to 64 bytes

The reason for this restriction is that the database name will eventually become a file in the file system.

Some database names are retained and can directly access these databases with special functions, such:


1. admin

From the perspective of permissions, this is a "root" database. If a user is added to this database, this user automatically inherits the permissions of all databases.

Some specific server commands can only be run from this database, such as listing all databases or disabling servers.

2. local

This database will not be copied and can be used to store any set limited to a single local server.

3. config

When MongoDB is used for fragment settings, the config database is used internally to save information about the fragment.



Important:

Put the database name in front of the Set Name and get the fully qualified name of the Set, called the namespace. For example, if

When the blog. posts set is used, the namespace Of This set is cms. blog. posts. The namespace cannot exceed 121 bytes,

In actual application, it should be smaller than 100 bytes.

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.