2017-09-20 2 views
1

みなさん、こんにちは、MongoDBでの検索でJoinQueryを実行するにはどうしたらいいですか?

私は顧客が口座の停止要求を送信することができますプロジェクトに取り組んでいますし、管理者として、私はリストを見ることができますし、ページ上でそれらを無効、同じページに、私は名前でフィルタリングするための検索フィルタを持っています、電子メールと電話番号。私は、顧客を保持するコレクションと失効要求を保存する別のコレクションを持っています。私は、クエリに参加して問題に直面しています

 

type DeactivationRequest struct { 
     Id       int   `json:"id" bson:"id"` 
     Uid       int   `json:"uid" bson:"uid"` 
     RequestTime     int64  `json:"request_time" bson:"request_time"` 
     Status      int   `json:"status" bson:"status"` 
     Note      string  `json:"note" bson:"note"` 
    } 

    type User struct { 
     Id       int   `json:"id" bson:"_id,omitempty"` 
     FirstName     string  `json:"first_name,omitempty" bson:"first_name,omitempty"` 
     LastName     string  `json:"last_name,omitempty" bson:"last_name,omitempty"` 
     EmailId     string  `json:"email_id,omitempty" bson:"email_id,omitempty"` 
     Password     string  `json:"password,omitempty" bson:"password,omitempty"` 
     PhoneNumber    string  `json:"phone_number,omitempty" bson:"phone_number,omitempty"` 
     AltPhoneNumber    string  `json:"alt_phone_number,omitempty" bson:"alt_phone_number,omitempty"` 
     Status      int   `json:"status" bson:"status"` 
     Role      string  `json:"role,omitempty" bson:"role,omitempty"` 
    } 

:私は、私はレコードを取得するために行くのlangで2つの構造体を以下している、選択クエリとのシンプルとして上場一部を行わなく、検索フィルタでリストを取得するには、問題に直面しています次のコードを書いた検索キーワードに基づいてレコードを取得します。下記のように、結果のため

 

    result := []bson.M{} 
     skip := 0 
     limit := 20 
     c := mongoSession.DB("database").C("deactivation_requests") 
     pipe := c.Pipe([]bson.M{bson.M{"$match": bson.M{ "status" : 0 }}, 
           bson.M{"$lookup": bson.M{ 
                "localField" : "_id", 
                "from"   : "users", 
                "foreignField" : "uid", 
                "as"   : "profile"}}, 
           bson.M{"$unwind": "$profile"}, 
           bson.M{"$skip": skip}, 
           bson.M{"$limit": limit},}) 
     err = pipe.All(&result) 
     if err != nil { 
      return result, err 
     } 

期待形式は次のとおりです。

これを行うための最善の方法は何ですか?

ありがとうございます。

<table> 
     <tr> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Phone Number</th> 
      <th>Note</th> 
     </tr> 
     <tr> 
      <td>Swati Sharma</td> 
      <td>[email protected]</td> 
      <td>987-999-9999</td> 
      <td>I don't want my acount</td> 
     </tr> 
     <tr> 
      <td>James Black</td> 
      <td>[email protected]</td> 
      <td>999-999-3120</td> 
      <td>I don't want my acount</td> 
     </tr> 
    </table> 
+0

検索のために、あなたの基準は、「ユーザー」のプロパティであれば、確かにそれは、「ユーザー」コレクションから検索する方が理にかなっているし、その後のみとそれらのユーザーまたはユーザーに参加している「deactivation_requests」を返します場合によっては可能性があります。これは、まずコレクションに参加してから、結果を照合して一致するユーザーを検索するよりも効率的です。 –

+0

@NeilLunnあなたの答えをありがとう。私は私の質問を更新してください見てください。 – Swati

答えて

0

こんにちは

私は私のクエリのための答えを見つけました。私は次のコードを試して、それは私のために働く。

 

    result := []bson.M{} 
      skip := 0 
      limit := 20 
      c := mongoSession.DB("database").C("deactivation_requests") 
      pipe := c.Pipe([]bson.M{ 
            bson.M{"$lookup": bson.M{ 
                 "localField" : "_id", 
                 "from"   : "users", 
                 "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": skip}, 
            bson.M{"$limit": limit},}) 
      err = pipe.All(&result) 
      if err != nil { 
       return result, err 
      } 

関連する問題