开发者

MongoDB/py-mongo for queries with date functions

开发者 https://www.devze.com 2022-12-20 02:41 出处:网络
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

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消