"MongoDB" in-depth understanding of the 10 points MongoDB should know

Source: Internet
Author: User

First, the objectIDthe Build

Each of the MongoDB documents requires a primary key. It must be unique for all documents in each collection. The primary key is stored in the document _id field. consists of three characters;

4c291856 238d3b 19b2 000001

4 -byte timestamp machine ID process ID counter 3333

Second, BSON

BSON is the binary format used to mark documents in MongoDB, which is both a storage format and a command format. All documents are stored as bson on disk, and all queries and commands are specified with the Bson document.

Db.users.find ({_id:objectid (' 4c291856238d3b19b2000001 ')})

Db.users.find ({_id: ' 4c291856238d3b19b2000001 '})

The results of both of these queries are completely different. Only one of them can query the matching _id field, which depends entirely on the document stored in the users collection as the BSON object ID or the BSON string that identifies the binary ID 16 .

Iii. Aggregation of command limits

In terms of usefulness,distinct and group have a very large limit. They cannot return a result set that exceeds 16M. the 16M limit is not the threshold imposed by these commands themselves, which is the size of all initial query results.

Assuming that distinct and group cannot handle your collection result set, you can only use map-reduce instead. Its result can be saved in the collection by non-inline return.

Iv. Processing of atomic documents

We know that MongoDB is not good at dealing with things, but what if the user really needs to query and update the same time to do? There is a tool you definitely don't want to miss, that's MongoDB 's findandmodify command.

The command agrees to atomically update the document and return it in the same call.

Db.collections.findAndModify (

{

Query:{},update:{},new:true or False

}

)

By default, thefindandmodify command returns the pre-update document, and if you return the modified document, set new to false.

V. Use of pairs of arrays$unset

Note that the result of using $unset on a single array element may not be the same as you would imagine. The result is simply setting the value of the element to null rather than deleting the entire element. To completely delete an array element, you can use $pull and $pop operators.

Vi. $addToSetand the$pushthe Difference

The function of both is to add a value to the array.

However, there is a difference between the two,$addToSet The value to be added assumes that there is no join operation, but the push simply adds a value, such as :

tags = ["Tools", "garden"]

Assume that the result of running db.collection.update ({},{$push: {tag:tools}}) is ["Tools", "garden". "Tools"]

Assume that the result of running db.collection.update ({},{$addToSet: {tag:tools}}) is unchanged

Vii. Sparse Index Creation

In a sparse index, only documents that have the value of the indexed key will appear, false assumptions create a sparse index, specifying {sparse:true} is possible.

For example :

Db.product.ensureIndex ({sku:1},{unique:true,sparse:true})

The Sku may exist as a null document.


Be careful when declaring indexes

Because it's easier to create an index, it's easy to accidentally create an index, assuming the dataset is very large. Construction can take a very long time. And there's no way to grow it.

It's best to sort this out at the same time to create the index more efficiently.

Nine, withExplain(true) specific query run schedule

User Db.collection.find (condition). Explain (true)

Ten, optimistic lock

Optimistic locking is concurrency control, which ensures that the device is completely updated without having to lock down records. The simplest way to understand it is to imagine a WiFiconnection wheremultiple users can edit a page at the same time. But you certainly do not want users to edit and update an outdated page, which is the ability to use the optimistic locking protocol. When users try to save their changes, a timestamp is added to the update operation. Assume that the value is older than the recently saved version number of this page, so the user is not updated.

This is used in MongoDB run $inc

"MongoDB" in-depth understanding of the 10 points MongoDB should know

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.