英文:
Search based on regular expression in mgo does not give required result
问题
<p>你好,我有以下的golang代码:</p>
<p>请看一下:</p>
<pre>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)
</pre>
<p>我在数据库中有一条记录,名字是"swati",姓氏是"sharma"。
当我搜索"swati"时,它正常工作,同样地,当我搜索"sharma"时,它也正常工作。</p>
<p>问题是当我搜索"swati sharma"时,它没有返回任何结果。
有人能告诉我如何实现这个输出吗?</p>
英文:
<p>Hello I have the following code in my golang:</p>
<p>Please take a look:</p>
<pre>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)
</pre>
<p>I have a record in database with first name "swati" and last name "sharma".
When I search "swati" then it works properly, similarly when I search "sharma"
it works properly.</p>
<p>Issue is when I search "swati sharma" then it does not return any result.
Can anybody tell how I can achieve this output?</p>
答案1
得分: 1
我在我的代码中进行了以下更改,并且它可以工作。
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)
英文:
<p>I made the following changes in my code and it works.</p>
<pre>
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)
</pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论