I am trying to find time required to perform a count() on a collection which is consisting of millions of testdata records, with following scenario:-
1) From 1st Mongo shell I am inserting millions of records into collection using a code
for (var i = 0; i < 10000000; ++i){
db.unicorns.insert({name: 'sampleName', gender: 'm', weight: '440' });
}
2) From 2ndMongo shell I am trying to find count() on that collection(Imp: while insertion is still getting executed on 1st Mongo Shell)
db.unicorns.count()
I researched but found that explain() and stats() cannot be applied to count() command.
some
I need to find out how much time it takes to count() when there are insertions going on collection(something like a live scenario)?
Is there any other good approach for doing this?
MongoDB has a built-in profiller that you can enable via:
db.setProfilingLevel(2)
Instead of '2' you can choose any option from the list bellow:
And you can see the results of your queries by checking the system.profile collection in MongoDB..
EDIT:
If you want to test performance you could use the following snippets of code that can be executed from the mongo console:
> for (var i = 0; i < 10000000; ++i) { db.countTest.insert({a: i % 10}) }
> db.countTest.ensureIndex({a:1})
> db.countTest.count({a: 1})
> db.countTest.count()
> db.countTest.find().count()
And my conclusions are as following:
So the more indexes your collection has the slower your query will be. If you count by _id it will be instant, if you have a composite index it will scale based on the number of indexes.