0

For example, we have two collections

users {userId, firstName, lastName}

votes {userId, voteDate}

I need a report of the name of all users which have more than 20 votes a day.

How can I write query to get data from MongoDB?

1

3 Answers 3

2

The easiest way to do this is to cache the number of votes for each user in the user documents. Then you can get the answer with a single query.

If you don't want to do that, the map-reduce the results into a results collection, and query that collection. You can then run incremental map-reduces that only calculate new votes to keep your results up to date: http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-IncrementalMapreduce

Sign up to request clarification or add additional context in comments.

Comments

2

You shouldn't really be trying to do joins with Mongo. If you are you've designed your schema in a relational manner.

In this instance I would store the vote as an embedded document on the user.

In some scenarios using embedded documents isn't feasible, and in that situation I would do two database queries and join the results at the client rather than using MapReduce.

Comments

0

I can't provide a fuller answer now, but you should be able to achieve this using MapReduce. The Map step would return the userIds of the users who have more than 20 votes, the reduce step would return the firstName and lastName, I think...have a look here.

Comments

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.