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

Source: Internet
Author: User

First, the objectIDthe Build

Each MongoDB document requires a primary key, which must be unique for all documents in each collection, and 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 these two queries are completely different, with only one query to the matching _id field, which is entirely dependent on the BSON object stored in the documents in the users Collection ID or the BSON string that identifies the binary ID 16 .

Iii. Aggregation of command limits

In terms of practicality,distinct and group have a big limit, and they cannot return a result set of more than 16M. the 16M limit is not the threshold imposed by these commands themselves, which is the size of all initial query results. If distinct and group cannot handle your collection result set, then you can only use map-reduce instead, Its results can be saved in the collection by non-inline returns.

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 operation? There is a tool you definitely don't want to miss, that's MongoDB 's findandmodify command. This command allows atomic updates to the document and is returned in the same call.

Db.collections.findAndModify (

{

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

}

)

By default, thefindandmodify command returns the pre-update document and, if the modified document is returned, sets the 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 add if it does not exist for the add operation, but the push adds only one value; for example :

tags = ["Tools", "garden"]

If you execute db.collection.update ({},{$push: {tag:tools}}) The result is ["Tools", "garden", "tools"]

If execution db.collection.update ({},{$addToSet: {tag:tools}}) The result is unchanged

Vii. Sparse Index Creation

Only documents that have a value in the index key will appear in the sparse index, and if you want to create a sparse index, specify {sparse:true}. 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 creating an index is simple, it is easy to accidentally create an index, which can take a long time to build if the dataset is large. And there's no way to grow it. It's best to sort this out at the same time to create an index.

Nine, withExplain(true) detailed query execution plan

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 optimistic locking protocol can be used, when the user tries to save their changes, will add a timestamp in the update operation, if the value is older than the last version of the page saved, the user is not updated.

This is used in mongodb execution $inc

MongoDB to learn more about the 10 points that MongoDB has to 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.