Find objects between two dates MongoDB

后端 未结 14 2055
余生分开走
余生分开走 2020-11-21 06:03

I\'ve been playing around storing tweets inside mongodb, each object looks like this:

{
\"_id\" : ObjectId(\"4c02c58de500fe1be1000005\"),
\"contributors\" :          


        
14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 06:26

    Python and pymongo

    Finding objects between two dates in Python with pymongo in collection posts (based on the tutorial):

    from_date = datetime.datetime(2010, 12, 31, 12, 30, 30, 125000)
    to_date = datetime.datetime(2011, 12, 31, 12, 30, 30, 125000)
    
    for post in posts.find({"date": {"$gte": from_date, "$lt": to_date}}):
        print(post)
    

    Where {"$gte": from_date, "$lt": to_date} specifies the range in terms of datetime.datetime types.

提交回复
热议问题