Getting started with MongoDB

Source: Internet
Author: User
Tags findone mongodb client mongodb server

Basic concepts of MongoDB

1. The document is the basic unit of data in MongoDB. It is similar to the row of a relational database (but more complex than the row)

2. a set can be seen as a table without a schema

3. A single MongoDB instance can accommodate multiple independent databases, each of which has its own set and permissions.

4. MongoDB comes with a simple but powerful JavaScript shell. This tool is very useful for managing MongoDB instances and operating data.

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

Details:

A) Documentation

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 document is also different:

{"Age": 30}

{"Age": 30}

MongoDB documents cannot have duplicate keys

{"Hello": "Hello World", "hello": "refactor"} This is incorrect.

 

B) Set

A set is a set of documents. If a document is equivalent to a row in a relational database, a set 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.

 

The Set Name Is A UTF-8 string that meets the following conditions.

1. The set name cannot be a Null String ""

2. The set name cannot contain \ 0 null characters. This character indicates the end of the Set Name.

3. The set name cannot start with "system.". This is the prefix reserved for the system set. For example, the system. Users set stores the user information of the database.

The system. namespaces set stores information about all database sets.

4. The set name cannot contain reserved characters "$"

 

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.

 

C) Database

Multiple documents in 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.

 

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.

 

D) MongoDB Shell

MongoDB comes with a javascript shell that can interact with MongoDB instances from the command line.

1. Run Shell

Shell is a fully functional javascript interpreter that can run any JavaScript program:

2. MongoDB Client

Shell is more important. It is the MongoDB client. When it is enabled, shell will connect to the test database of the MongoDB server and

The database connection value is the global variable dB, which is the main entry point for accessing MongoDB through shell.

Shell also has non-JavaScript syntax extensions to facilitate SQL shell users, such:

Select the database to use:

Use test

You can use dB variables to access the set. For example, DB. Users returns the users set of the current database.

 

Complete in shell, crud

1. Add

The insert function adds a document to the set. For example, to store a blog post, first create a local variable post, which indicates

JavaScript Object.

2. Read

Find will return all the documents in the set. If you only want to view one document, you can use findone

Find and findone can accept the conditions in the form of query documents, and query documents using the conditions. When using find, shell automatically

Up to 20 matching documents can be displayed

3. Update

Update accepts two parameters: one is the limitation of the document to be updated, and the other is the new document. For example, add comments to the blog.

4. Delete

Remove permanently deletes a document from the database. If you do not call a parameter, it deletes all documents in a set. It can also

Delete a document as a condition. For example:

 

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.