title: MongoDB - OR function with a lot of conditions
body: <p>I'm having a strange behavior when I make a MongoDB query with a lot of conditions in an $or function.</p>
  
  <p>Basically what happens is that 100 results are cycled instantaneously in the DBCursor, and the following 5000 results are cycled just after 2 minutes!</p>
  
  <p>Here’s the code (method that makes the query):</p>
  
  <pre><code>public Object getMongoData(String aDbName, String aCollectionName, DBObject aQueryObj, DBObject aSortObj, boolean aFindOne,
    DBObject aFieldsFilter, int aLimitStartPosition, int aLimitNumberResults, boolean aCreateIndex) throws Exception
    {
      // String aDbName MY_DB
      // String aCollectionName = MY_COLLECTION
      // DBObject aQueryObj = {"$or":[{"_id":"id1"},…,{"_id":"id5000"}]}
      // DBObject aSortObj = null
      // boolean aFindOne = false
      // DBObject aFieldsFilter = null
      // int aLimitStartPosition = 0
      // int aLimitNumberResults = 0
      // boolean aCreateIndex = false
  
      Object resultObj = null;
      DBObject query;
      DBObject filter;
  
      if (aQueryObj == null)
      {
        query = new BasicDBObject();
      }
      else
      {
        query = aQueryObj;
      }
  
      if (aFieldsFilter == null)
      {
        filter = new BasicDBObject();
      }
      else
      {
        filter = aFieldsFilter;
      }
  
      DBCollection collection = loadCollection(aDbName, aCollectionName);
      if (collection != null)
      {
        if (aFindOne)
        {
          resultObj = collection.findOne(query, filter);
        }
        else
        {
          if ((aSortObj != null) &amp;&amp; !aSortObj.keySet().isEmpty())
          {
            if (aCreateIndex)
            {
              collection.createIndex(aSortObj);
            }
  
            resultObj = collection.find(query, filter).skip(aLimitStartPosition).limit(aLimitNumberResults).sort(aSortObj);
          }
          else
          {
            resultObj = collection.find(query, filter).skip(aLimitStartPosition).limit(aLimitNumberResults);
          }
        }
      }
  
      return resultObj;
    }
  </code></pre>
  
  <p>Code (code that invoques the previous method and cycles the results):</p>
  
  <pre><code>dbCursor = (DBCursor) getMongoData(super.getMainDatabaseName(), aCollectionName, aQueryObj, aSortObj, false, aFilterObj);
        if (dbCursor != null)
        {
          int i = 0;
  
          while (dbCursor.hasNext())
          {
            DBObject dbObject = dbCursor.next();
  
            logger.debug("SEARCH FINISHED COUNT: " + dbCursor.count() + "\t CURRENT: " + i);
            i++;
          }
        }
  </code></pre>
  
  <p>Any idea why this happens?</p>
  
  <p>Thank you</p>