golang中如何根据url从collection中查找数据?
How to find the data from collection according to url in golang?
我正在从数据库中检索数据,当用户点击 url 时,例如 http://localhost:8080/api/v1/customer?keyword=dhiman
然后它在 collection 中搜索数据,如果有任何字段匹配然后它将检索该数据。如果用户输入 url 之类的短 http://localhost:8080/api/v1/customer?keyword=dhi
那么它也会检索匹配的数据,我将如何解决这个问题。我尝试了如下代码:-
客户结构
type Customer struct {
Id int `json:"id" bson:"_id"`
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"`
}
type Customers []Customer
函数
func GetCustomers(c *gin.Context) {
value := c.Query("keyword")
fmt.Println(value)
response := ResponseControllerList{}
conditions := bson.M{"last_name":value}
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,
// dataCount,
nil,
}
}
GetResponseList(c, response)
}
模型页面中的 GetCustomerListing 函数:-
func GetCustomerListing(customerQuery interface{}) (result Customers, err error) {
mongoSession := config.ConnectDb()
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
getCollection := mongoSession.DB(config.Database).C(config.CustomerCollection)
err = getCollection.Find(customerQuery).Select(bson.M{"password": 0}).All(&result) //.Skip(skip).Limit(limit)
if err != nil {
return result, err
}
return result, nil
}
collection 图片
我得到的答案是使用 mongodb 中的 $or
。
在 monogdb 中有一个运算符叫做 or $or
它检查所有字段的值并产生结果。
使用了 bson.RegExis
。因为它会匹配或检查与它从用户那里接收到的数据相似的数据。
情况有变。条件是:-
conditions := bson.M{"$or": []bson.M{
bson.M{"first_name": bson.RegEx{value,""}},
bson.M{"last_name": bson.RegEx{value,""}},
bson.M{"email": bson.RegEx{value,""}},
bson.M{"phone_number": bson.RegEx{value,""}},
}}
查询有变化
我正在从数据库中检索数据,当用户点击 url 时,例如 http://localhost:8080/api/v1/customer?keyword=dhiman
然后它在 collection 中搜索数据,如果有任何字段匹配然后它将检索该数据。如果用户输入 url 之类的短 http://localhost:8080/api/v1/customer?keyword=dhi
那么它也会检索匹配的数据,我将如何解决这个问题。我尝试了如下代码:-
客户结构
type Customer struct {
Id int `json:"id" bson:"_id"`
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"`
}
type Customers []Customer
函数
func GetCustomers(c *gin.Context) {
value := c.Query("keyword")
fmt.Println(value)
response := ResponseControllerList{}
conditions := bson.M{"last_name":value}
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,
// dataCount,
nil,
}
}
GetResponseList(c, response)
}
模型页面中的 GetCustomerListing 函数:-
func GetCustomerListing(customerQuery interface{}) (result Customers, err error) {
mongoSession := config.ConnectDb()
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
getCollection := mongoSession.DB(config.Database).C(config.CustomerCollection)
err = getCollection.Find(customerQuery).Select(bson.M{"password": 0}).All(&result) //.Skip(skip).Limit(limit)
if err != nil {
return result, err
}
return result, nil
}
collection 图片
我得到的答案是使用 mongodb 中的 $or
。
在 monogdb 中有一个运算符叫做 or $or
它检查所有字段的值并产生结果。
使用了 bson.RegExis
。因为它会匹配或检查与它从用户那里接收到的数据相似的数据。
情况有变。条件是:-
conditions := bson.M{"$or": []bson.M{
bson.M{"first_name": bson.RegEx{value,""}},
bson.M{"last_name": bson.RegEx{value,""}},
bson.M{"email": bson.RegEx{value,""}},
bson.M{"phone_number": bson.RegEx{value,""}},
}}
查询有变化