在我的网页中,一堆数据将显示在表格中。为了更轻松地查找数据,网页还具有搜索栏。页面上的数据看起来像:-
Date & Time | Activity | Ip Address
07/04/2018 | Booking has been updated by Customer | 202-213-10-10
07/04/2018 | There is modification by Customer1 | 202-213-10-10
与我在上表中显示的数据相同。当我搜索任何activity, ip address, date & time
时,它将向我显示匹配的记录。示例我将搜索modification
,然后仅显示第二条记录。
Date & Time | Activity | Ip Address
07/04/2018 | There is modification by Customer1 | 202-213-10-10
我想在搜索栏中进行一些修改,这意味着如果我搜索该数据,那么客户将更改哪些数据,然后它还会向我显示匹配数据。
结构
type Log struct {
Id bson.ObjectId `json:"_id" bson:"_id,omitempty"`
Role string `json:"role" bson:"role"`
Date string `json:"date" bson:"date"`
IpAddress string `json:"ipaddress" bson:"ipaddress"`
Booking interface{} `json:"booking" bson:"booking"`
}
type Logs []Log
func GetLog(c *gin.Context) {
value := c.Query("value")
conditions := bson.M{"$or": []bson.M{
bson.M{"role": bson.RegEx{".*" + value + ".*", "i"}},
bson.M{"date": bson.RegEx{".*" + value + ".*", "i"}},
bson.M{"ipaddress": bson.RegEx{".*" + value, "i"}},
bson.M{"booking.address": bson.RegEx{".*" + value, "i"}},
bson.M{"booking.email": bson.RegEx{".*" + value, "i"}},
bson.M{"booking.worker":bson.RegEx{".*"+value, "i"}},
bson.M{"booking.custmer":bson.RegEx{".*"+value, "i"}},
}}
data, err := models.GetAllLogs(conditions)
dataCount, err := models.GetRecordsCount(config.LogCollection, conditions)
fmt.Println(data)
}
现在假设我在
之类的mongodb中手动添加数据
db.table_name.insert({"date":"07/04/2018", "role":"There is modification by Customer1", "ipaddress":"202-213-10-10", "booking":["bedrooms":1, "bathroom":2, "customer":"abc", "worker":1, "address":"addresss is there", "email":"abc@abc.com"]})
它将数据插入数据库,然后当我按其客户搜索此数据时,它将向我显示记录,就像我正在按customer name: abc
搜索数据,然后它将向我显示记录上面我提到这是正确的方法?如果是,那么如果我增加预订对象中的字段数,那么该bson.M{"booking.field_name":bson.RegEx{".*"+value, "i"}},
代码行将添加到该字段的每次添加中,为避免这种情况,我必须添加此代码。
建议一些例子。
谢谢您的帮助。