title: How to fetch all data quickly from mongodb using indexing field without using limit
body: <p>I have around <strong>~2 million</strong> documents in my database, when i made an query in mongo for  which looks like:</p>
  
  <pre><code>db.collection.ensure_index("location")
  db.collection.find({"location.name": {"$regex": 'norway', "$option": 'i'}}, {"_id:0", 'userid': 1, 'username': 1})
  </code></pre>
  
  <p>mongo document sample for location searching:</p>
  
  <pre><code>Mongodb document: {"location": {id:3235726, name: Oslo, Norway }}
  </code></pre>
  
  <p>In indexing i am not specified <strong>cache_time</strong> part, it uses default. Now, it shows me data after <strong>2 or 4 seconds</strong> which is very bad, and the most worse part when it doesn't find any location then it took around <strong>25seconds</strong> for searching operation. I mapped this time using python <strong>time.time()</strong> method. Then i put a <strong>limit()</strong> on query only for 100 documents, then it works a bit well. But what is the use case for me if i put a limit there, using <strong>skip()</strong> is very expansive while searching. </p>
  
  <p>On other part if i am searching data using userid like, it doesn't contain regex type query:</p>
  
  <pre><code>db.collection.ensure_index("userid")
  db.collection.find({"userid": 1213444}, {"_id:0", 'user_rank': 1, 'username': 1})
  </code></pre>
  
  <p>It works very efficiently, as id is integer and very fast. Only difference is i am using '<strong>like</strong>' query in location query part with regex, and i studied on mongo docs <strong>regex</strong> is not bad in terms of performance.</p>
  
  <p>Should i go for increasing physical memory on my server which is currently 1.5G for using indexing efficiently or using <strong>Apache Solr</strong> search engine which index all my mongo docs through mongo_connector and sync data from mongodb to solr (Indexing data directly to solr is very expansive in terms of memory )?</p>
  
  <p>I tried to put all efforts in my problem statement, if there is anything wrong with my problem statement then let me know, i can try my best to improve it.</p>
  
  <p>Edit: Should i go for elasticsearch with mongo? because with index or without index search result is too slow, like maximum time it shows <strong>260ms</strong> which is too high</p>