1

As I installed MongoDB on a Linux server, I found that Map & Reduce function is much slower than db.count() and db.find(). For example, if I execute script

for(var i=0; i<4789302; ++i){
db.collection5.insert({ item: 
    "journal", 
    qty: 25, 
    tags: ["blank", "red"], 
    size: {  h: 16, w: 21, uom: "cm" }
    })
}

to insert some small JSON documents into a collection, I find that the execution time for db.collection5.count({"size.h":16}) is around 1.66 seconds, but the execution time for 478 MB dataset with 2394651 rows cost around 28 seconds. Besides that, if I add indexes on some related fields, then db.collection.find() and db.collection.count() could be much faster, but the execution time for Map & Reduce is still the same.

Can someone explain the reason for above phenomenon?

mrsrinivas
  • 34,112
  • 13
  • 125
  • 125
Jack
  • 101
  • 3
  • Take a look at this response: https://stackoverflow.com/a/15157188/1048800 from another user. – jgatjens Oct 19 '17 at 06:00
  • Also related; [MongoDB Count() vs. Aggregation](https://stackoverflow.com/questions/33181878/mongodb-count-vs-aggregation). Bottom line is the only way an "index" has an effect on a `mapReduce` operation is within the options for `query` or `sort` in selection. Just like with `.find()` or `.count()` ( which really just wraps `.find().count()` ). A "cursor" is a completely different thing and obtaining a count from the cursor returned is way different to processing the whole collection and calculating numbers. Especially when an index can match. – Neil Lunn Oct 19 '17 at 06:06

0 Answers0