golang中如何根据查询字符串中输入的数据获取数据?
how to get the data according to the data entered in the query string in golang?
我正在从 mongodb 中的集合中检索数据,这些数据与使用 golang 在查询字符串中输入的数据相匹配。
有两种情况
案例 1
假设如果用户搜索单个字段,如 first_name
、last_name
等。然后将通过点击 url http://localhost:8080/api/v1/customer?keyword=puneet
来检索数据
案例 2
在情况 2 中,假设我们在 mongodb 集合中有两个字段,例如 first_name : puneet
、last_name : jindal
等。如果用户将在查询字符串中输入first_name
和last_name
这两个字段
示例点击 url 就像 http://localhost:8080/api/v1/customer?keyword=puneet%20kumar
然后没有数据检索。
我为此尝试了以下代码。但没有成功
这个的结构是:
type Customer struct {
Id int `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
FirstName string `json:"first_name" bson:"first_name"`
LastName string `json:"last_name" bson:"last_name"`
Email string `json:"email" bson:"email"`
PhoneNumber string `json:"phone_number" bson:"phone_number"`
}
检索数据的函数:
func GetCustomers(c *gin.Context) {
value:= c.Query("keyword")
fmt.Println(value)
response := ResponseControllerList{}
conditions := bson.M{"$or": []bson.M{
bson.M{"first_name": bson.RegEx{".*" + value, "i"}},
bson.M{"last_name": bson.RegEx{".*" + value, "i"}},
bson.M{"email": bson.RegEx{".*" + value, "i"}},
bson.M{"phone_number": bson.RegEx{".*" + value, "i"}},
}}
data, err := models.GetCustomerListing(conditions)
if err != nil {
response = ResponseControllerList{
config.FailureCode,
config.FailureFlag,
config.FailureMsg,
nil,
nil,
}
} else {
response = ResponseControllerList{
config.SuccessFlag,
config.SuccessFlag,
config.SuccessMsg,
data,
nil,
}
}
GetResponseList(c, response)
}
我做了解决方案
value:= c.Query("keyword")
fmt.Println(value)
response := ResponseControllerList{}
name := strings.Replace(value, " ", "|", -1)
conditions := bson.M{"$or": []bson.M{
bson.M{"first_name":bson.RegEx{"(?i).*"+name+".*", "i"} },
bson.M{"last_name": bson.RegEx{ "(?i).*"+name+".*", "i"} },
bson.M{"email": bson.RegEx{".*" + value, "i"}},
bson.M{"phone_number": bson.RegEx{".*" + value, "i"}},
}}
查询中的更改
我正在从 mongodb 中的集合中检索数据,这些数据与使用 golang 在查询字符串中输入的数据相匹配。
有两种情况
案例 1
假设如果用户搜索单个字段,如 first_name
、last_name
等。然后将通过点击 url http://localhost:8080/api/v1/customer?keyword=puneet
案例 2
在情况 2 中,假设我们在 mongodb 集合中有两个字段,例如 first_name : puneet
、last_name : jindal
等。如果用户将在查询字符串中输入first_name
和last_name
示例点击 url 就像 http://localhost:8080/api/v1/customer?keyword=puneet%20kumar
然后没有数据检索。
我为此尝试了以下代码。但没有成功
这个的结构是:
type Customer struct {
Id int `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
FirstName string `json:"first_name" bson:"first_name"`
LastName string `json:"last_name" bson:"last_name"`
Email string `json:"email" bson:"email"`
PhoneNumber string `json:"phone_number" bson:"phone_number"`
}
检索数据的函数:
func GetCustomers(c *gin.Context) {
value:= c.Query("keyword")
fmt.Println(value)
response := ResponseControllerList{}
conditions := bson.M{"$or": []bson.M{
bson.M{"first_name": bson.RegEx{".*" + value, "i"}},
bson.M{"last_name": bson.RegEx{".*" + value, "i"}},
bson.M{"email": bson.RegEx{".*" + value, "i"}},
bson.M{"phone_number": bson.RegEx{".*" + value, "i"}},
}}
data, err := models.GetCustomerListing(conditions)
if err != nil {
response = ResponseControllerList{
config.FailureCode,
config.FailureFlag,
config.FailureMsg,
nil,
nil,
}
} else {
response = ResponseControllerList{
config.SuccessFlag,
config.SuccessFlag,
config.SuccessMsg,
data,
nil,
}
}
GetResponseList(c, response)
}
我做了解决方案
value:= c.Query("keyword")
fmt.Println(value)
response := ResponseControllerList{}
name := strings.Replace(value, " ", "|", -1)
conditions := bson.M{"$or": []bson.M{
bson.M{"first_name":bson.RegEx{"(?i).*"+name+".*", "i"} },
bson.M{"last_name": bson.RegEx{ "(?i).*"+name+".*", "i"} },
bson.M{"email": bson.RegEx{".*" + value, "i"}},
bson.M{"phone_number": bson.RegEx{".*" + value, "i"}},
}}
查询中的更改