Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: Calling Count() vs tracking counts in a collection

I am moving our messaging system to MongoDB and am curious what approach to take with respect to various stats, like number of messages per user etc. In MS SQL database I have a table where I have different counts per user and they get updated by trigger on corresponding tables, so I can for example know how many unread messages UserA has without calling an expensive SELECT Count(*) operation.

Is count function in MongoDB also expensive? I started reading about map/reduce but my site is high load, so statistics has to update in real time, and my understanding is that map/reduce is time consuming operation.

What would be the best (performance-wise) approach on gathering various aggregate counts in MongoDB?

like image 934
Andrey Avatar asked May 19 '11 06:05

Andrey


People also ask

What is count () in MongoDB?

The count() method counts the number of documents that match the selection criteria. It returns the number of documents that match the selection criteria. It takes two arguments first one is the selection criteria and the other is optional.

Is count faster than find MongoDB?

find({}). count() more fast then collection.

Is MongoDB count slow?

Count queries, indexed or otherwise, are slow due to the fact that MongoDB still has to do a full b-tree walk to find the appropriate number of documents that match your criteria.


1 Answers

If you've got a lot of data, then I'd stick with the same approach and increment an aggregate counter whenever a new message is added for a user, using a collection something like this:

counts

{
    userid: 123,
    messages: 10
}

Unfortunately (or fortunately?) there are no triggers in MongoDB, so you'd increment the counter from your application logic:

db.counts.update( { userid: 123 }, { $inc: { messages: 1 } } )

This'll give you the best performance, and you'd probably also put an index on the userid field for fast lookups:

db.counts.ensureIndex( { userid: 1 } )
like image 122
Chris Fulstow Avatar answered Sep 20 '22 18:09

Chris Fulstow