0

Indexing is possible while fetching some particular records, sorting, ordering etc but suppose a collection contains lot many documents and it is taking time to fetch them all and display. So how to make this query faster using indexing? Is it possible using indexing? If yes then is it the best way or is there any other way too?

EDIT 1

Since indexing can't be used in my case. What is the most efficient way to write a query to fetch millions of records?

Edit 2

This is my mongoose query function for fetching data from some collection. If this collection has millions of data then obviously it will affect performance so how will you use indexing in this case for good performance?

Info.statics.findAllInfo = function( callback) {
this.aggregate([{$project:{
    "name":"$some_name",
    "age":"$some_age",
    "city":"$some_city",
    "state":"$some_state",
    "country":"$some_country",
    "zipcode":"$some_zipcode",
    "time":{ $dateToString: { format: "%Y-%m-%d %H:%M:%S ", date: "$some_time" } }
}},
{
    $sort:{
        _id:-1
    }
}],callback);
};

I haven't tried lean() method yet due to some temporary issue. But still i would like to know whether it will help or not?

node_saini
  • 727
  • 1
  • 6
  • 22
  • 1
    Indexing only helps in the cases you mention. However, I see you tagged this with Mongoose; the biggest way to speed up a large Mongoose query is to include `.lean()` in the query chain as shown [here](http://stackoverflow.com/a/31576032/1259510). Not sure if that's what you're looking for though. – JohnnyHK May 26 '16 at 14:42
  • You need to provide more details on your query and environment. For example: the specific version of MongoDB server, the query with the output of [`explain(true)`](https://docs.mongodb.com/manual/reference/method/cursor.explain/), and a snippet of code showing how you are iterating the results. Without further information it is hard to suggest why your query might be running slow or whether there could be a suitable index. – Stennie Jun 05 '16 at 21:43
  • @Stennie I think you did not understand my question. Please read it again and also the comments :) – node_saini Jun 13 '16 at 08:46
  • Yes, I find the question unclear. You haven't provided any information on the query or code, so I'm not sure how we can help. You also say that "indexing can't be used in my case" .. why is that? If all that you are after is Mongoose's `lean()` method, the current title and description are mostly irrelevant. – Stennie Jun 13 '16 at 08:57
  • @Stennie I have edited my question for your reference. Hope its clear now and you can suggest something if possible :) – node_saini Jun 13 '16 at 09:19
  • Thanks! Code examples definitely help. Is your aggregation query simplified here or is that the actual aggregation? Since you're apparently not doing any processing (aside from formatting a date string), one obvious improvement would be to do a normal `find()`. Since your aggregation doesn't have any `$match` criteria this is going to be retrieving the full collection data and no additional indexes are going to be relevant aside from possibly helping the `$sort`. You've sorted by `_id: -1`, so the default `_id` index would be an obvious candidate. What version of MongoDB are you using? – Stennie Jun 13 '16 at 11:54
  • @Stennie There are lot of columns in table but i want to fetch only some so i am projecting only those fields using aggregation. I am gonna sort it according to time in future as i want latest records first. Also i think that has nothing to do with indexing as its just simple sorting. I think now you have got my problem. Mongo version is 3.2.3 – node_saini Jun 14 '16 at 07:29

0 Answers0