Why collection.find ({}) takes more than 9 seconds for 250 objects (MongoMapper)

I run the following query, and it takes an average of 9 seconds to return the results. There are no filters on it, so I'm not sure if the index will help. Why does it work so slow? There are only 250 objects and only 4 fields (all text).

Country.collection.find({},:fields => ['country_name', 'country_code']).to_json

"cursor":"BasicCursor",
"nscanned":247,
"nscannedObjects":247,
"n":247,
"millis":0,
"nYields":0,
"nChunkSkips":0,
"isMultiKey":false,
"indexOnly":false,
"indexBounds":{},
"allPlans":[{"cursor":"BasicCursor","indexBounds":{}}]

The processor, memory and disk on the machine do not even notice the execution of the request. Any help would be greatly appreciated.

+5
source share
1 answer

Create indexes in the 'country_name' parameters using:

db.countries.ensureIndex({country_name:1});

This will greatly speed up your query. You can learn more about indexes here.

PS- "it", , " ", "", :

db.countries.find({}, {'country_name' : 1, 'country_code' : 1}).forEach(printjson)

, :

>use databaseName;
> db.setProfilingLevel(2); // 2 tell the profiler to catch everything happened inside the DB

,

> db.system.profile.find()

, .

+3

All Articles