Introduction to "two" MongoDB

Source: Internet
Author: User
Tags mongo shell

Here are some of the basic concepts of MongoDB:

    • A document is a basic unit of data in MongoDB, similar to a row in a relational database.
    • Collection, which is a container for storing documents, similar to a table in a relational database.
    • A single instance of MongoDB accommodates multiple databases, each with its own collection and permissions.
    • Each document has a special "_id", which is unique within the collection where the document is located.

To be easy to understand, let's compare the concept in MongoDB with the relational database:

First, the document

Documentation is the core concept of MongoDB. Multiple key values are placed together in an orderly manner as a document. A simple example:

1   {"Greeting": "Hello World", "foo": 2}   {"foo":-"greeting": "Hello World"}3   {"foo": 15}4   {" Foo ": 15}
5 {"Foo": "15"}
    • Key name: cannot contain spaces and special characters ' $ ' and '. ', while the keys at the beginning of the underscore are reserved
    • Key names are case-sensitive, as the above 3 and 4 are different documents
    • Key names cannot be duplicated in the document
    • Document difference types, such as 4 and 5 above, have different types of values, so they belong to separate documents
    • The value of a key is not only a string but also another type

Two, set

A collection is a set of documents, and if a document is a row in a relational database, the collection is like a table. The collection has the following characteristics:

1. No mode

Collections are modeless, which means that collections can store a wide variety of documents. So is not MongoDB only need a collection to do it? The answer is, here are some reasons:

    • Easy to manage: If a variety of documents are placed in a collection, it is a nightmare for developers or administrators.
    • Improved efficiency: Checking different types of documents in a collection is certainly not as fast as a collection of individual types.
    • Improve indexing efficiency

2. Naming

We can identify the collection by name, and here are some constraints:

    • Cannot be an empty string, such as ""
    • Cannot contain spaces
    • Cannot start with system.
    • Cannot contain special characters, such as $

A common practice for organizing collections is to divide sub-collections by namespace, such as developing a blog

Third, the database

A database consists of multiple collections, one instance containing multiple databases, and one application for one database. Naming rules for databases:

    • Cannot be an empty string
    • Cannot include special characters such as spaces
    • Should all lowercase
    • Cannot use system reserved, such as Admin,local,config

Related operations on the database:

#查看当前数据库 > dbtest# switch Current database > Use localswitched to DB local> dblocal

  Get help:

> Help Db.help ()-On DB Methods Db.mycoll.help () Help on collection methods                   Sh.help () sharding helpers rs.help () replica set helpers help admin Administrative help and connect connecting to a DB Help keys key s                     Hortcuts Help Misc Misc Things to know help Mr MapReduce show DBS                   Show database names show collections show collections in current database show users Show users in current database show profiles show most recent system.profile entries with Ti Me >= 1ms show logs show the accessible logger names show log [name] prints out T He last segment of the log in memory, ' global ' is the default use <db_name> set current database Db.foo . Find () LIST objects in collection foo db.foo.find ({a:1}) List objects in Foo where a = = 1 it Result of the last line evaluated;                         Use to further iterate dbquery.shellbatchsize = X Set default number of items to display on shell exit  Quit the MONGO shell>

"Two" MongoDB introduction

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.