go - Search based on regular expression in mgo does not give required result -
hello have following code in golang:
please take look:
type user struct { id int `json:"id" bson:"_id"` firstname string `json:"first_name" bson:"first_name"` lastname string `json:"last_name" bson:"last_name"` emailid string `json:"email_id" bson:"email_id"` password string `json:"password" bson:"password"` phonenumber string `json:"phone_number" bson:"phone_number"` altphonenumber string `json:"alt_phone_number" bson:"alt_phone_number"` gender string `json:"gender" bson:"gender"` note string `json:"note" bson:"note"` address string `json:"address" bson:"address"` aptno string `json:"apt_no" bson:"apt_no"` city string `json:"city" bson:"city"` zipcode string `json:"zipcode" bson:"zipcode"` } query := bson.m{ "role" : "customer", "status" : 1, "$or": []bson.m{ bson.m{"first_name":bson.regex{".*"+keyword+"*.", "i"} }, bson.m{"last_name": bson.regex{".*"+keyword+"*.", "i"} }, bson.m{"email_id": bson.regex{".*"+keyword, "i"} }, bson.m{"phone_number": bson.regex{".*"+keyword, "i"} }, bson.m{"alt_phone_number": bson.regex{".*"+keyword, "i"} }, }} err = c.find(query).all(&result)
i have record in database first name "swati" , last name "sharma". when search "swati" works properly, when search "sharma" works properly.
issue when search "swati sharma" not return result. can tell how can achieve output?
i made following changes in code , works.
name := strings.replace(keyword, " ", "|", -1) conditions := bson.m{ "role" : config.providerrole, "status" : status, "$or": []bson.m{ bson.m{"first_name":bson.regex{"(?i).*"+name+".*", "i"} }, bson.m{"last_name": bson.regex{ "(?i).*"+name+".*", "i"} }, bson.m{"email_id": bson.regex{".*"+keyword, "i"} }, bson.m{"phone_number": bson.regex{".*"+keyword, "i"} }, bson.m{"alt_phone_number": bson.regex{".*"+keyword, "i"} }, }} err = c.find(query).all(&result)
Comments
Post a Comment