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

    评论

报告相同问题?