How can I query MongoDB with date range using mgo and Go?

后端 未结 3 1956
花落未央
花落未央 2020-12-10 02:15

Hi I have a collection named \"my_sales\" having fields product_name, price, sale_date.

My doc looks like

{
    \"_id\" : ObjectId(\"5458b6ee09d76eb7         


        
3条回答
  •  醉酒成梦
    2020-12-10 03:03

    I have new way to query date range:

    // convert to date
    fromTime := time.Unix(1509358405981/1000, 0)     
    
    // Convert date to ObjectID with time    
    fromObjectBson := bson.NewObjectIdWithTime(fromTime)
    
    // condition     
    bson.M{"_id":bson.M{"$gt": fromObjectBson}} 
    

    This will speedup your query.

提交回复
热议问题