"MongoDB Learning note 23" MongoDB indexed objects and arrays

Source: Internet
Author: User

MongoDB allows deep-inside documents to index nested fields and arrays; nested objects and array fields can be used with top-level fields in a composite index, in most cases consistent with the behavior of the "normal" indexed fields.

I. Indexing nested documents

For example, the document in the collection has the following format,

> Db.post.findOne ({"username": "Sid"}) {"_id": ObjectId ("54aff7f43bd1048e7b585e39"), "username": "Sid    "," loc ": {" IP ":" 1.2.3.4 "," City ":" Springfield "," State ":" NY "} } >

An index on the "loc" City is required to increase the query speed for this loc.city field:

> Db.post.ensureIndex ({"Loc.city": 1}) {"createdcollectionautomatically": false, "Numindexesbefore": 1, "Numindexesafter": 2, "OK": 1} >

This way, you can create an index of any depth, such as an index on X.Y.Z.A.B.C.

However, the index established on the subdocument "Loc" is different from the index on a field "loc.city" that is built on the subdocument:

(1) The index created on the entire subdocument will only improve the query speed of the entire subdocument; that is, the sub-document index will only work if the query that exactly matches the subdocument (including the field order);

(2) Only query loc.city field, index loc.city will work, other conditions index loc.city not work;


Second, the index on the array

(1) It can be seen that the cost of establishing an index on an array field is relatively large, because each time the deletion, the update will refresh each index, consuming the resources of the server too much;

(2) A specific individual index can be made for an element in an array field, reducing the number of indexes; For example, an index on a votes in the Nineth element in the array field comments:

> Db.post.ensureIndex ({"Comment.10.votes": 1})

Similarly, the index will only work if the exact match comment.10.votes query.

Three, multi-key index

If you create an index on an array field, this index is called a multi-key index (Multikey).

Multi-key index with the explain function, you can see that the value of the "Ismultikey" field is true, and a multi-key index is slower than a non-multi-key index;



This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1601473

"MongoDB Learning note 23" MongoDB indexed objects and arrays

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.