MongoDB Multikeys Index

Source: Internet
Author: User

MongoDB provides an interesting "multikey" feature that can automatically index arrays of an object's values.

However, the results with Multikeys are not surprising at first.

Let's take a look at the test example:
Save multiple test statements to a collection:

  1. Db. fjx. save ({name:"Fjx", Tags :['Weate','Hot','Record','Cmdl']})
  2. Db. fjx. save ({name:"Fjx1", Tags :['Weather 1','Hot','Record','Cmdl']})
  3. Db. fjx. save ({name:"Fjx2", Tags :['Weate2','Hot','Record','Cmdl']})
  4. Db. fjx. save ({name:"Fjx3", Tags :['Weather 3','Hot','Record','Cmdl']})
Create an index for the tags array!
  1. > Db. fjx. find ({tags: {$ regex:"^ Weather"}). Explain ()
  2. {
  3. "Cursor":"BtreeCursor tags_1 multi",
  4. "Nscanned": 4,
  5. "NscannedObjects": 4,
  6. "N": 4,
  7. "Millis": 0,
  8. "NYields": 0,
  9. "NChunkSkips": 0,
  10. "IsMultiKey":True,
  11. "IndexOnly":False,
  12. "IndexBounds":{
  13. "Tags":[
  14. [
  15. "Weather",
  16. "Weathes"
  17. ],
  18. [
  19. /^ Weather /,
  20. /^ Weather/
  21. ]
  22. ]
  23. }
  24. }
By querying the explain starting with weather, it uses the multi index. We can insert another one.
  1. Db. fjx. save ({name:"Fjx4", Tags :['Weather 4','Weather 5','Record','Cmdl']})
  2. > Db. fjx. find ({tags: {$ regex:"^ Weather"}). Explain ()
  3. {
  4. "Cursor":"BtreeCursor tags_1 multi",
  5. "Nscanned": 5,
  6. "NscannedObjects": 5,
  7. "N": 4,
  8. "Millis": 0,
  9. "NYields": 0,
  10. "NChunkSkips": 0,
  11. "IsMultiKey":True,
  12. "IndexOnly":False,
  13. "IndexBounds":{
  14. "Tags":[
  15. [
  16. "Weather",
  17. "Weathes"
  18. ],
  19. [
  20. /^ Weather /,
  21. /^ Weather/
  22. ]
  23. ]
  24. }
  25. }

We can see that there are 5 nscanned records and 4 n records. Note that, especially when you create an index for an array, because the index is created for the array, it creates an index for the elements in the array, so it traverses the elements that match the index. n indicates the number of successful searches.

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.