Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:33:56+00:00 2026-05-24T06:33:56+00:00

Sorry if this has been asked before. I couldn’t find a definitive answer. Can

  • 0

Sorry if this has been asked before. I couldn’t find a definitive answer. Can I query on a mongodb index if my query contains the $or operator? My query looks something like this:

    // find everything in the collection
    $cursor = $collection->find(array(
   '$or' => array( 
        array('album_id' => array(
            '$in' => $this->my_album_ids
            ),
        'type' => array(
            '$in' => array('like','comment')
            )
         ),
       array(
    'user_id' => (int)session_item('user_id'),
        'type' => 'message',
        'reply' => 'no'
         )
       ),
        'timestamp' => array('$gt' => (int)$since)))->sort(array('timestamp'=>-1))->skip($start)->limit($amount);  

The Example is in PHP, but I guess this is applicable to any language.

Update:

The following are my indexes, but the above query does not use them. It looks right to me though.

    $collection->ensureIndex(array(
        'album_id' => 1,
        'type' => 1,
        'timestamp' => -1,
    ));

    $collection->ensureIndex(array(
        'user_id' => 1,
        'type' => 1,
        'reply' => 1,
        'timestamp' => -1,
    ));

Here is my explain()

Array
(
    [cursor] => BasicCursor
    [nscanned] => 12
    [nscannedObjects] => 12
    [n] => 6
    [scanAndOrder] => 1
    [millis] => 0
    [nYields] => 0
    [nChunkSkips] => 0
    [isMultiKey] => 
    [indexOnly] => 
    [indexBounds] => Array
        (
        )

    [allPlans] => Array
        (
            [0] => Array
                (
                    [cursor] => BasicCursor
                    [indexBounds] => Array
                        (
                        )

                )

        )

    [oldPlan] => Array
        (
            [cursor] => BasicCursor
            [indexBounds] => Array
                (
                )

        )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:33:57+00:00Added an answer on May 24, 2026 at 6:33 am

    Yes, an $or query will use indexes as appropriate. For example :

    > db.test.ensureIndex({a:1})
    > db.test.ensureIndex({b:1})
    > db.test.find({$or:[{a:1}, {b:2}]}).explain()
    {
            "clauses" : [
                    {
                            "cursor" : "BtreeCursor a_1",
                            "nscanned" : 0,
                            "nscannedObjects" : 0,
                            "n" : 0,
                            "millis" : 0,
                            "nYields" : 0,
                            "nChunkSkips" : 0,
                            "isMultiKey" : false,
                            "indexOnly" : false,
                            "indexBounds" : {
                                    "a" : [
                                            [
                                                    1,
                                                    1
                                            ]
                                    ]
                            }
                    },
                    {
                            "cursor" : "BtreeCursor b_1",
                            "nscanned" : 0,
                            "nscannedObjects" : 0,
                            "n" : 0,
                            "millis" : 1,
                            "nYields" : 0,
                            "nChunkSkips" : 0,
                            "isMultiKey" : false,
                            "indexOnly" : false,
                            "indexBounds" : {
                                    "b" : [
                                            [
                                                    2,
                                                    2
                                            ]
                                    ]
                            }
                    }
            ],
            "nscanned" : 0,
            "nscannedObjects" : 0,
            "n" : 0,
            "millis" : 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this has been asked before, I did check but couldn't find anything...
Sorry if this has been asked before but I've tried searching and can't find
Sorry if this has been asked before, but I couldn't find a solution to
Sorry if this has been asked before, I searched but couldn't find anything. I'm
Sorry if this has been asked before, I can't seem to find anything. I'm
Sorry if this has been asked before, but I can't find a clear enough
I'm sorry if this has been asked before - but I couldn't find a
I'm sorry if this question has been asked already, but I couldn't find it
I searched here for the answer. I'm sorry if this has been asked before
I'm sorry if this question has been asked before, but I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.