dqx36753 2018-10-13 05:35
浏览 160

如何使用golang在查询mongodb中验证时间

Here I'm building a query for getting a booking from database and I'm making a query for it but according to conditions I want to build a query and make a condition on the time field that if time is not equal to and less then zero then it will join with the query condition other wise not. following is the condition I'm using and I want to validate the time:-

mongoSession := ConnectDb()
defer mongoSession.Close()
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
getCollection := sessionCopy.DB(Database).C(Collection) 
condition := bson.M{
    "status":           1,
    "category": bson.M{"$in": services},
    "method":   bson.M{"$in": value},
    "date":     date,
    "location": loc,
    "end_time":         bson.M{"$lte": currTime},
} 

I want to validate the end_timeby following:-

if currTime > 0{
   "end_time":bson.M{"$lte": currTime},
}
// Something like that

Can anyone tell me how I will perform this?

  • 写回答

1条回答 默认 最新

  • douxiegan6468 2018-10-13 05:55
    关注

    You use map for it like this:-

    condition := make(map[string]interface{})
    condition["status"] = 1
    condition["category"] = bson.M{"$in": services}
    condition["method"] = bson.M{"$in": value}
    condition["date"] = date
    condition["location"] = loc
    if currTime > 0 {
        condition["end_time"] = bson.M{"$lte": currTime}
    }
    

    and pass the condition in the query

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂