0

model checkin:

checkin
  _id
  interest_id
  author_id

I've got a collection of checkins (resolved by simple "find" query) I'd like to count the number of checkins for each interest. What makes the task a bit more difficult - we should count two checkins from one person and one interest as one checkin.

AFAIK, group operations in mongo are performed by map/reduce query. Should we use it here? The only idea I've got with such an approach is to aggregate the array of users for each interest and then return this array's length.

EDIT I ended up with not using map/reduce at all, allthough Emily's answer worked fine & quick. I have to select only checkins from last 60 minutes, and there shouldn't be too many results. So I just get all of them to Ruby driver, and do all the calculation's on ruby side. It's a bit slower, but much more scalable and easy-to-understand.

best, Roman

1 Answer 1

1

Map reduce would probably be the way to go for this and you could get the desired results with two map reduces.

In the first, you could remove duplicate author_id and interest_id pairs.

  • key would be author_id and interest_id
  • values would be checkin_id

The second map reduce would just be a count of the number of checkins by a given author_id.

  • key would be author_id
  • value would be checkin_id count
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, but, I guess, you mean a count of the number of checkins by a given interest_id instead of author_id?
Yes, I'm sorry, I just reread your question and you would count by interest_id, not author_id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.