简体   繁体   中英

Mongodb slow count query

I have a collection containing 2 million records like this:

{
    "_id" : ObjectId("5a2bcdde0fab483921eeda9b"),
    "body" : "a big text is here ///",
    "txt_filename" : "/home/ehsan/data/pubmed_data/txt_data/Nutr_Res/Nutr_Res_2013_Mar_33(3)_188-194.txt",
    "processed" : true,
    "xml_filename" : "",
    "type" : "comm"
}

When I want to execute this command:

db.articles.count({processed: true})

it took couple of minutes to see the results. What's the reason? Do I need to put an index on the table? I can query this in Elasticsearch in just a few milliseconds. What's the problem with MongoDB?

You need to create indexes on fields ( processed ) inorder to get the faster result.

create index:

db.articles.createIndex( { "processed": 1 } );

You can refer below link for more info. mongodb count vs find with count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM