【问题标题】:How do I perform Joinquery with search in MongoDB and go?如何在 MongoDB 中执行 Joinquery 并进行搜索?
【发布时间】:2018-03-01 00:54:20
【问题描述】:

大家好,

我正在开展一个项目,客户可以发送帐户停用请求,作为管理员,我可以在页面上查看列表并停用它们,在同一页面上,我有一个搜索过滤器,可以按姓名、电子邮件和电话号码进行过滤。我有一个集合来保存客户,另一个集合来保存停用请求。我已经完成了列表部分,因为它使用选择查询很简单,但面临通过搜索过滤器获取列表的问题,我在 Go lang 中有以下 2 个结构来获取记录:

类型 DeactivationRequest 结构 { ID int `json:"id" bson:"id"` uid int `json:"uid" bson:"uid"` RequestTime int64 `json:"request_time" bson:"request_time"` 状态 int `json:"status" bson:"status"` 注意字符串`json:"note" bson:"note"` } 类型用户结构{ ID int `json:"id" bson:"_id,omitempty"` 名字字符串 `json:"first_name,omitempty" bson:"first_name,omitempty"` 姓氏字符串 `json:"last_name,omitempty" bson:"last_name,omitempty"` EmailId 字符串`json:"email_id,omitempty" bson:"email_id,omitempty"` 密码字符串`json:"password,omitempty" bson:"password,omitempty"` 电话号码字符串 `json:"phone_number,omitempty" bson:"phone_number,omitempty"` AltPhoneNumber 字符串 `json:"alt_phone_number,omitempty" bson:"alt_phone_number,omitempty"` 状态 int `json:"status" bson:"status"` 角色字符串 `json:"role,omitempty" bson:"role,omitempty"` }

我遇到了连接查询的问题,以根据我编写了以下代码的搜索关键字获取记录。

结果:= []bson.M{} 跳过:= 0 限制:= 20 c := mongoSession.DB("数据库").C("deactivation_requests") 管道 := c.Pipe([]bson.M{bson.M{"$match": bson.M{ "status" : 0 }}, bson.M{"$lookup": bson.M{ "localField" : "_id", “来自”:“用户”, "foreignField" : "uid", "as" : "profile"}}, bson.M{"$unwind": "$profile"}, bson.M{"$skip": 跳过}, bson.M{"$limit": 限制},}) 错误 = pipe.All(&result) 如果错误!= nil { 返回结果,错误 }

结果的预期格式如下:

最好的方法是什么?

提前致谢。

<table>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Phone Number</th>
            <th>Note</th>
        </tr>
        <tr>
            <td>Swati Sharma</td>
            <td>swati@swatii.com</td>
            <td>987-999-9999</td>
            <td>I don't want my acount</td>
        </tr>
        <tr>
            <td>James Black</td>
            <td>james@jamess.com</td>
            <td>999-999-3120</td>
            <td>I don't want my acount</td>
        </tr>
    </table>

【问题讨论】:

  • 如果您的搜索条件是“用户”的属性,那么从“用户”集合中搜索然后只返回加入这些用户的“deactivation_requests”肯定更有意义或用户视情况而定。这比先加入集合然后暴力搜索加入的结果以找到匹配的用户要高效得多。
  • @NeilLunn 感谢您的回答。我已经更新了我的问题,请看一下。

标签: mongodb go


【解决方案1】:

你好

我找到了我的查询的答案。我已经尝试了以下代码,它对我有用。

结果:= []bson.M{} 跳过:= 0 限制:= 20 c := mongoSession.DB("数据库").C("deactivation_requests") 管道 := c.Pipe([]bson.M{ bson.M{"$lookup": bson.M{ "localField" : "_id", “来自”:“用户”, "foreignField" : "uid", "as" : "profile"}}, bson.M{"$unwind": "$profile"}, bson.M{"$match": bson.M{"$or": []bson.M{ bson.M{"profile.first_name":bson.RegEx{"(?i).*"+name+".*", "i"} }, bson.M{"profile.last_name": bson.RegEx{ "(?i).*"+name+".*", "i"} }, bson.M{"profile.email_id": bson.RegEx{"(?i).*"+name+".*", "i"} }, bson.M{"profile.phone_number": bson.RegEx{".*"+keyword+".*", "i"} }, bson.M{"profile.alt_phone_number": bson.RegEx{".*"+keyword+".*", "i"} }, }}}, bson.M{"$skip": 跳过}, bson.M{"$limit": 限制},}) 错误 = pipe.All(&result) 如果错误!= nil { 返回结果,错误 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2017-09-16
    • 2016-10-05
    相关资源
    最近更新 更多