2016-11-08 5 views
0

私のアプリケーションのバックエンドとしてgo langを使用し、mongoDBをdatabase.Iとして使用して、名前とプロジェクトを比較する問題に直面しています。そのleave.Sinceのための証明書をプロジェクト私は従業員の構造体から私はパイプとプロジェクトを使用して実装したい少しの情報が必要なので。は、mgoを使用してネストされた配列を投影する必要があります

type (
     Employee struct { 
      Name    string 
      Password   string 
      EmailAddress  string 
      Position   string 
      Gender    string 
      Nationality  string 
      Department   string 
      MaritalStatus  string 
      Approvedby   string 
      JoinDate   time.Time 
      ConfirmationDate time.Time 
      EndDate   time.Time 
      Leave    []*LeaveInfo 
     } 
     LeaveInfo struct { 
      Total  float64 
      Id   int 
      Days   float64 
      From  time.Time 
      To   time.Time 
      Status  string 
      Certificate []*CertificateInfo 
     } 
     CertificateInfo struct { 
      FileName string 
      FileType string 
      FileSize int 

     } 

{ 
    "_id" : ObjectId("58213e14927a62f3cf04e05b"), 
    "name" : "string", 
    "password" : "string", 
    "emailaddress" : "string", 
    "position" : "string", 
    "gender" : "string", 
    "maritalstatus" : "string", 
    "approvedby" : "string", 
    "nationality" : "german", 
    "department" : "account", 
    "joindate" : ISODate("2016-09-19T00:00:00.000Z"), 
    "confirmationdate" : Date(-62135596800000), 
    "enddate" : Date(-62135596800000), 
    "Leave" : [ 
     { 
      "total" : 20.0, 
      "id" : 0, 
      "days" : 0.0, 
      "type" : "", 
      "from" : ISODate("2016-12-12T00:00:00.000Z"), 
      "to" : ISODate("2016-12-12T00:00:00.000Z"), 
      "status" : "", 
      "certificate" : [ 
        { 
        "filename" : "malaysia", 
        "filetype" : ".zip", 
        "filesize" : 1234 
       }, 
       { 
        "filename" : "singapore", 
        "filetype" : ".zip", 
        "filesize" : 1234 
       } 
      ] 
     }, 
     { 
      "total" : 19.0, 
      "id" : 1, 
      "days" : 1.0, 

      "from" : ISODate("2016-12-12T00:00:00.000Z"), 
      "to" : ISODate("2016-12-12T00:00:00.000Z"), 
      "applieddate" : ISODate("2016-11-08T02:53:38.902Z"), 
      "status" : "Processing", 
      "approveddate" : Date(-62135596800000), 
      "certificate" : [ 
       { 
        "filename" : "germany", 
        "filetype" : ".zip", 
        "filesize" : 1234 
       }, 
       { 
        "filename" : "england", 
        "filetype" : ".zip", 
        "filesize" : 1234 
       } 
      ] 
     }, 
     { 
      "total" : 18.0, 
      "id" : 2, 
      "days" : 1.0, 
      "mdays" : 0.0, 
      "type" : "annualleave", 
      "daytype" : "FullDay", 
      "from" : ISODate("2016-12-12T00:00:00.000Z"), 
      "to" : ISODate("2016-12-12T00:00:00.000Z"), 
      "applieddate" : ISODate("2016-11-08T05:36:21.579Z"), 
      "status" : "Processing", 
      "approveddate" : Date(-62135596800000), 
      "certificate" : [ 
        { 
        "filename" : "india", 
        "filetype" : ".zip", 
        "filesize" : 1234 
        }, 
        { 
        "filename" : "france", 
        "filetype" : ".zip", 
        "filesize" : 1234 
        } 
       ] 
      } 
     ] 
    } 

golangで私のコードを次のようにデータベース構造がある

pipe3 := c.Pipe([]bson.M{ 
      { 
       "$match": bson.M{ 

        "name":name 
       }, 
      }, 
      { 
       "$unwind": "$leave", 
      }, 

      { 
       "$project": bson.M{ 
        "_id":   false, 

        "name":   1, 
        "Id":   "$leave.id", 
        "Total":  "$leave.total", 
        "Days":   "$leave.days", 
        "Status":  "$leave.status", 
       }, 
      }, 
     }) 

を次のようになり、ここでの問題は、私は、証明書のすべての情報を取得する方法を知りませんでその特定の休暇に関連しています。証明書は少なくとも2つのインデックスを持つ配列です...

I need an output similar to this. 
        "name":  "John", 
        "Id":   "1", 
        "Total":  "10.0", 
        "Days":   "2.0", 
        "Status":  "Process", 
        "Certificate" : [ 
         { 
        "filename":"certificate1", 
        "filesize":"size1" 
        }, 
        { 
         "filename":"certificate2", 
         "filesize":"size2" 
        } 
         ] 


       "name":  "John", 
       "Id":   "2", 
       "Total":  "8.0", 
       "Days":   "2.0", 
       "Status":  "Process", 
       "Certificate" : [ 
        { 
       "filename":"certificate1", 
       "filesize":"size1" 
       }, 
       { 
        "filename":"certificate2", 
        "filesize":"size2" 
       } 
        ] 

はこの可能使用project.orです上記文書の構造例に使用this.Appreciate任意のhelp.Please.Thanks

+0

「証明書」を$プロジェクトステージに追加します。 –

答えて

1

を行うには、他の方法があり、あなただけのように以下の$projectを通じてcertificateを公開しなければなりません:あなたはすでにLeave$unwindを使用したよう

pipeline := []bson.M{ 
       {"$match": bson.M{"name":"string"}}, 
       {"$unwind": "$Leave"}, 
       {"$project": bson.M{ 
         "_id":   false, 
         "name":   1, 
         "Id":   "$Leave.id", 
         "Total":  "$Leave.total", 
         "Days":   "$Leave.days", 
         "Status":  "$Leave.status", 
         "Certificate": "$Leave.certificate"}, 
       }, 
      } 
pipe := collection.Pipe(pipeline) 
response := []bson.M{} 
err = pipe.All(&response) 

、あなたは離脱要求ごとに個々の文書を持っています。上記集計の出力は、次のようになります。

{ 
    "name": "string", 
    "Id": 0, 
    "Total": 20, 
    "Days": 0, 
    "Status": "", 
    "Certificate": [ 
    { 
     "filename": "malaysia", 
     "filetype": ".zip", 
     "filesize": 1234 
    }, 
    { 
     "filename": "singapore", 
     "filetype": ".zip", 
     "filesize": 1234 
    } 
    ] 
}, 
{ 
    "name": "string", 
    "Id": 1, 
    "Total": 19, 
    "Days": 1, 
    "Status": "Processing", 
    "Certificate": [ 
    { 
     "filename": "germany", 
     "filetype": ".zip", 
     "filesize": 1234 
    }, 
    { 
     "filename": "england", 
     "filetype": ".zip", 
     "filesize": 1234 
    } 
    ] 
}, 
{ 
    "name": "string", 
    "Id": 2, 
    "Total": 18, 
    "Days": 1, 
    "Status": "Processing", 
    "Certificate": [ 
    { 
     "filename": "india", 
     "filetype": ".zip", 
     "filesize": 1234 
    }, 
    { 
     "filename": "france", 
     "filetype": ".zip", 
     "filesize": 1234 
    } 
    ] 
} 

各休暇申請には、休暇に関連する独自の対応証明書があります。

+0

助けてくれてありがとうございます...申し訳ありません。このように使用すると、証明書が表示されません。値を残して....もう一つの疑問を持っています。もし私が名前を確認して、私が求めている形式で、その詳細をすべての従業員の詳細を表示していないなら、それは可能です.... –

+0

私はあなたのサンプル文書は投稿したとおりに正確に表示されます。同じ結果が得られない場合は、コードと関連があります。 '$ leave'と' $ Leave'の大文字を確認してください。それに失敗し、入力文書と上記のコードの正確な出力を投稿してください。私はあなたの第二の質問を理解していない、あなたは例を使ってそれを詳述できますか? –

+0

ありがとう...私のコードで間違ったことをしました。それを修正した後、とてもうれしく思います。私はあなたの答えを正しいものとして与えます。もう一度感謝します... –

関連する問題