MongoDB documents, collections, Databases (ii)

Source: Internet
Author: User
Tags create index reserved



To understand the terms of MongoDB, you can compare it to a relational database:



First, the document



  Overview



The document is the core concept of MongoDB and is the basic unit of data, very similar to a row in a relational database. In MongoDB, a document is represented as an ordered set of key-value pairs. MongoDB uses JavaScript shells, which are typically tagged with the style of the object in JavaScript, as follows:

{"title": "hello!"}
{"title": "hello!", "recommend": 5}
{"title": "hello!", "recommend": 5, "author": {"firstname": "paul", "lastname": "frank"}}
As you can see from the above example, the value of the document has different data types, and it can even be a complete embedded document (the author of the last example is represented by a complete document, which defines firstname and lastname. Of course It can also contain more other information even in embedded documents.)

Description

Documents are case sensitive and data type, so the following two sets of documents are different:

{"recommend": "5"}
{"recommend": 5}

{"Recommend": "5"}
{"recommend": "5"}
MongoDB documents are ordered in key-value pairs, the following documents are different:

{"title1": "hello!", "title2": "Mongo"}
{"title2": "Mongo", "title1": "hello!"}
MongoDB documents cannot have duplicate keys. The following documents are illegal:

{"title": "hello!", "title": "Mongo"}
The value in the document can be not only a string, but also other data types (or embedded in other documents)

The key is a string, the key can use any UTF-8 character

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

And $ as reserved characters, usually should not appear in the key

Keys beginning with an underscore "_" are usually reserved

Create Creating a document is very simple. You can create a document record in the database by inserting a sentence.
> db.blogs.insert ({"title": "hello!"})
If the database and blogs collection are not created before this statement is executed, the database and collection will be created separately and the document will be inserted at the same time.

Delete

> db.blogs.remove () // Delete all documents in the collection.
> db.blogs.remove ({"title": "hello!"}) // Delete the document with the specified condition, the current statement deletes the document with "title" as "hello!"
Second, the collection
A collection is a collection of documents, which is equivalent to a data table in a relational database.

Dynamic mode

Collection is dynamic. What does that mean? Specifically, the documents in a collection can be of various types. For example, the following two documents can be stored in the same collection:

{"title": "hello!"}
{"recommend": 5}
It can be seen that the above two documents are not only worth different types, but also the keys are completely different. This is very different from the data structure that can only store the same model in a table in a relational database. But this also creates a problem: since any document can be stored in a collection, what is the necessity of the existence of multiple collections? In fact, this can be understood corresponding to the relational data table. We can create a table to hold the above mentioned title and recommended columns, but there is always a column that is NULL. This is only the case of two columns, if there are countless columns, then this situation is very bad. So it is not difficult to figure out why there are multiple collections in a database, which should have at least the following points:

The data is confusing. Developers need to distinguish that only certain types of documents are returned for each query, or leave this distinction to the application that processes the query results. This will cause great trouble for development and maintenance.

performance. Querying on different collections is much faster than querying different data in one collection.

The data is more centralized. The same type of documents are placed in a collection, the data is more centralized, when querying the data. Less disk seek operation is required and the efficiency is higher.

Use indexes more efficiently. The index is defined according to the collection. When creating an index, you need to use the additional structure of the document. Putting only one type of document in a collection can index the collection more efficiently.

Naming rules

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

 2. Cannot contain null character \ 0

 3. Cannot start with "system." This is the prefix reserved by the system collection

 4. Collection name cannot contain reserved characters $

 A habit of organizing collections is to separate them and divide the sub-collections near the namespace, such as system.Users, system.indexes

Common commands

Show collections View which collections exist in the current database, will display a list of collection names. As shown:

show collections View which collections exist in the current database, and display a list of collection names. As shown:

help () Get a list of executable commands on the collection. The execution statement is as follows:

 db.users.help ()
insert (obj) inserts a document into the collection.

drop () deletes the current collection and cannot be restored after deletion.

dropIndex (index) deletes the index on the collection, when the parameter is empty, deletes all indexes (except the index on _id)

ensureIndex (keypattern [, options]) create index

update (query, object [, upsert_bool, multi_bool]) update the documents in the collection that meet the conditions

find ([query, fields]) Query documents that meet the conditions according to the conditions

Of course there are many commands not listed here, but you can easily view the commands that can be executed on the collection through the help () command.

Third, the database
Multiple documents constitute a collection, and multiple collections constitute a database. A MongoDB instance can host multiple databases, and each database can have from 0 to multiple collections. Shown is the local file of the database used on my machine:

 

Description

Each database has corresponding data files and namespace files. The file prefix is the name of the database, the suffix .ns indicates the namespace file, and the suffix ending in .0, .1 and other numbers indicates the data file.

The size of the data file starts from 64MB (this is the result seen on 64-bit Windows Server 2012, other environments may be slightly different), the new data file is twice as large as the previous file. So you can see that the size of chen.0 is 64MB, the size of chen.1 is 128MB, and the size of chen.2 is 256MB.

The file uses MAP for memory mapping, which maps all data files to memory, but it is only virtual memory. Only when this piece of data is accessed will it be swapped into physical memory.

Each data file will be divided into data blocks one by one, and the blocks are linked by a doubly linked list.

In the namespace file, the storage information metadata of each namespace is saved, including its size, number of blocks, position of the first block, position of the last block, linked list of deleted blocks, and index information.

Naming rules

The database name is a UTF-8 string, up to 64 characters
Cannot be an empty string
It cannot contain ", /, \,., *, <,>,:, |,?, $, (A space), \ 0 (null character), basically only letters and numbers in ASCII can be used.
Database names are case-sensitive, even in case-insensitive file systems. For simplicity: all lowercase.
Common commands

show dbs View the databases that exist in the current MongoDB instance, show the list of database names and the amount of disk space occupied by the database. As shown:



 2.db Check which database is currently in use. As shown:



 3.use xxx Switch the currently used database. When using a non-existent database, the database data file and namespace file will not be created immediately, but the corresponding database will be created when the first file is inserted into the database. At this point, collections have similar characteristics.

 4.db.dropDatabase () Delete the currently used database. After deleting the currently used database, db still points to the name of the deleted database, you can switch through use; if you do data insertion without switching, a database with the same name will be re-established, but it is not the original database. Despite having the same name, it is possible to have the same collection and document.

System reserved database

admin: This is the root database, add a user to the database, the user will automatically inherit all database permissions
local: The data in this database will never be copied, and can be used to store any collection limited to a single server of local data
config: During fragmentation, the config database is used internally to save fragmentation information
Put the database name into the collection name and get the fully qualified name of the collection, called the namespace. The length of the namespace cannot exceed 121 bytes, and the actual usage should be less than 100 bytes.

MongoDB documents, collections, databases (2)

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.