views:

380

answers:

1

Im looking to use a document database such as MongoDB but looking through the documents I cant find much on queries that involve date functions. For example lets say that I'm asking one of the following questions of the DB:

  • "Tell me all the people who bought a product on tuesday"
  • "Get me all sales and group by month"

They are random questions but essentially they could be anything that has date functions. Would you have any idea how I would go about this?

Thanks, Chris.

+1  A: 

For the first query the best bet would be to do a range query for dates in between the start and end of tuesday. Something like:

db.foo.find({"purchase_date": {"$gt": monday_midnight, "$lte": tuesday_midnight}})

This will be nicer syntactically when the following case is finished, so might want to vote for it: http://jira.mongodb.org/browse/SERVER-465

For the second you'll probably want to check out either PyMongo's group or map_reduce methods, either of which can accomplish aggregation like that.

mdirolf