About MongoDB documents, collections, databases

Source: Internet
Author: User

Documentation 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:

1 {"title": "Hello!" }2 {"title": "Hello!", "recommend": 5}3 {"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, even a complete inline document (the last example of author is represented by a complete document, and the document defines FirstName and LastName. Of course, you can include more information and even embedded documents within the embedded document.

Description

The document is case-sensitive and data-type, so the following two sets of documents are different:

1 {"Recommend": "5"}2 {"Recommend": 5}34 {"Recommend": "5"  }5 {"Recommend": "5"}

MongoDB documents cannot have duplicate keys. The following documents are illegal:

1 {"title": "Hello!", "title": "Mongo"}
Action Creation

Creating a document is as simple as inserting a statement to create a document record in the database.

1 > Db.blogs.insert ({"title": "Hello!"})

If the database and the Blogs collection are not created before this statement is executed, the databases and collections are created separately, and the document is inserted.

Delete
1 > Db.blogs.remove ()   //  Delete all documents in the collection.  2 > Db.blogs.remove ({"title": "Hello!"})   // deletes the document for the specified condition, and the current statement deletes "title" as "Hello!" The document. 
Collection

A collection is a set of documents that corresponds to a data table in a relational database.

Dynamic mode

The collection is in dynamic mode. What do you mean? Specifically, a collection of documents can be of all kinds. For example, the following two documents can be stored entirely in the same collection:

1 {"title": "Hello!" }2 {"Recommend": 5}

As you can see, the above two documents are not only worthy of different types, but also the same key. This is quite different from the data structures in a table in a relational database that can only hold the same model. But this also creates the question: Since a collection can hold arbitrary documents, what is the need for multiple collections to exist? This can actually be understood in relation to relational data tables, and we can create a table containing the title and recommend columns mentioned above, but there is always one column that is null. This is just a case of two columns, which can be very bad if there are countless columns. So it's not hard to figure out why there are multiple collections in a database that should have at least the following points:

    1. Data clutter. The developer distinguishes between each query returning only a particular type of document, or handing the distinction to an application that processes the query results. This can be a big hassle for development and maintenance.

    2. Performance. Querying on different sets is much faster than querying different data in a collection.

    3. Data is more centralized. Documents of the same type are placed in a collection, the data is more concentrated, and the data is queried. Fewer disk seek operations are required and more efficient.

    4. More efficient use of indexes. Indexes are defined according to the collection. When you create an index, you need to use the additional structure of the document. You can index a collection more efficiently by placing only one type of document in a collection.

Common commands
    1. Show collections see which collections exist in the current database and will present a list of names for the collection. As shown in the following:

    2. Help () Gets the list of executable commands on the collection. Execute the following statement:

      1 db.users.help ()
    3. Insert (OBJ) inserts a document into the collection.

    4. Drop () deletes the current collection and is not recoverable after deletion.

    5. Dropindex (index) deletes the index on the collection, when the argument is empty, removes all indexes (except the index on _ID)

    6. Ensureindex (Keypattern[,options]) creating an index

    7. Update (Query,object[,upsert_bool,multi_bool]) updates a document in the collection that meets the criteria

    8. Find ([Query,fields]) search for documents that meet criteria based on criteria

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

Database

Multiple documents form a collection, and multiple collections form a database. A MongoDB instance can host multiple databases, and each database can have 0 to many collections. The local file for the database used on my machine is shown:

Description
    1. Each database has corresponding data files and namespace files. The prefix of the file is the name of the database, the suffix. NS represents the namespace file, and the suffix ends with a number of. 0,. 1, indicating the data file.

    2. The size of the data file starts at 64MB (this is the result that you see on 64-bit Windows Server 2012, other environments may differ), and the new data file is one-fold larger than the previous file. So can see that the size of the chen.0 is 64mb,chen.1 size is 128mb,chen.2 is 256MB.

    3. The file uses mmap for memory mapping, which maps all data files into memory, but only virtual memory, which is only exchanged in material memory when the data is accessed.

    4. Each data file is divided into one block of data, and the block is connected to the block by a two-way chain.

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

Common commands
    1. Show DBS View the databases that exist in the current MongoDB instance, showing the list of database names and the amount of disk space consumed by the database. As shown in the following:

    2. DB verifies which database is currently in use. As shown in the following:

    3. Use XXX to toggle the currently used database. When you use a nonexistent database, you do not immediately create the database's data file and namespace file, but instead create the corresponding database when you insert a file into the database for the first time. At this point, the collection has similar characteristics.

    4. Db.dropdatabase () deletes the currently used database. After deleting the currently used database, the DB still points to the deleted database name, can be toggled by use, if you do not switch the data insert operation, will re-establish a database of the same name, but it is not the original database, although the same name, there may be the same collection and document.

Resources:

https://docs.mongodb.org/manual/

"MongoDB authoritative Guide" (second edition)

About MongoDB documents, collections, databases

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.