MongoDB indexing Tips #1: Find the latest news from friends

Source: Internet
Author: User
We have implemented a limited fix for MongoDB2.2 and 2.4 limited fixes in version 2.2. The following query statement will work as you wish and return the result within milliseconds: db. posts. find ({userId :{$ in: [12, 24, 56,…]}). Sort ({date:-1}). limit (-100) So you may have thought this was perfect ...... Unfortunately

MongoDB 2.2 and 2.4 limited fixes 2.2. The following query statement returns the result in milliseconds as you wish: db. posts. find ({userId: {$ in: [12, 24, 56,…] }). Sort ({date:-1}). limit (-100) So you may think this is perfect ...... Unfortunately

Limited fixes for MongoDB 2.2 and 2.4

Version 2.2 provides a limited solution. The following query statement will work as you wish and return results within milliseconds:

Db. posts. find ({userId: {$ in: [12, 24, 56,…] }). Sort ({date:-1}). limit (-100)

So you may think that this is perfect ...... Unfortunately, there is a major limitation that any extra predicate will disable the optimization mechanism. Any extra predicate must be an equation (or "$ in") on a field. This field is part of the index and is located in the index field ("date" in this example). to the left. If you want to see the posts of a specific attribute (for example, "private: False"), you must be careful when constructing the index:

Db. posts. find ({userId: {$ in: [12, 24, 56,…], Private: False }}). sort ({date:-1}). limit (-100)

It must open the index above "{userId: 1, private: 1, date: 1. Some different types of predicate assertions, or the opposite field is not covered by the correct part of the index, so this will bring you back to the very bad and slow situation.

This problem will be solved by rebuilding the internal API of index 2.6, which should be exactly what you expect!

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.