在mgo中基于正则表达式进行搜索没有给出所需的结果。

huangapple go评论140阅读模式
英文:

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:&quot;id&quot; bson:&quot;_id&quot;
FirstName string json:&quot;first_name&quot; bson:&quot;first_name&quot;
LastName string json:&quot;last_name&quot; bson:&quot;last_name&quot;
EmailId string json:&quot;email_id&quot; bson:&quot;email_id&quot;
Password string json:&quot;password&quot; bson:&quot;password&quot;
PhoneNumber string json:&quot;phone_number&quot; bson:&quot;phone_number&quot;
AltPhoneNumber string json:&quot;alt_phone_number&quot; bson:&quot;alt_phone_number&quot;
Gender string json:&quot;gender&quot; bson:&quot;gender&quot;
Note string json:&quot;note&quot; bson:&quot;note&quot;
Address string json:&quot;address&quot; bson:&quot;address&quot;
AptNo string json:&quot;apt_no&quot; bson:&quot;apt_no&quot;
City string json:&quot;city&quot; bson:&quot;city&quot;
Zipcode string json:&quot;zipcode&quot; bson:&quot;zipcode&quot;
}

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, &quot; &quot;, &quot;|&quot;, -1)
	conditions := bson.M{ &quot;role&quot; : config.ProviderRole, 
							&quot;status&quot; : status, 
							&quot;$or&quot;: []bson.M{ 
								bson.M{&quot;first_name&quot;:bson.RegEx{&quot;(?i).*&quot;+name+&quot;.*&quot;, &quot;i&quot;} },
                                bson.M{&quot;last_name&quot;: bson.RegEx{ &quot;(?i).*&quot;+name+&quot;.*&quot;, &quot;i&quot;} }, 
                                bson.M{&quot;email_id&quot;: bson.RegEx{&quot;.*&quot;+keyword, &quot;i&quot;} }, 
                                bson.M{&quot;phone_number&quot;: bson.RegEx{&quot;.*&quot;+keyword, &quot;i&quot;} }, 
                                bson.M{&quot;alt_phone_number&quot;: bson.RegEx{&quot;.*&quot;+keyword, &quot;i&quot;} }, 
                            }}
err = c.Find(query).All(&amp;result)

</pre>

huangapple
  • 本文由 发表于 2017年9月12日 17:39:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/46172984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定