从我的前端,用户可以应用基于日期范围或目的,或两者的过滤器。如何生成MongoDB过滤器,以便如果用户传入日期范围,它仅将其用作过滤器?如果它是only purpose,那么它只使用purpose;如果它是both,那么它应用两个过滤器。我用的是golang。这是我的密码
var filterData map[string]interface{}
if purpose != "" {
filterData = bson.M{
"purpose": purpose,
}
var filterDate map[string]interface{}
if //condition for date range{
filterDate = bson.M{
"paymentDate": bson.M{
"$gte": startdate,
"$lt": enddate,
},
}
}
//here i cant apply both
cursor, err = collection.Find(conn.DatabaseContext, findQuery)