2016-11-11 16 views
1

バックエンドではgo langを使い、データベースにはmongoDBを使います。私は埋め込み配列に最後のドキュメントを挿入しようとしているので、インデックスを知らずに最後の配列インデックスのドキュメントを取得することができます。今、私は従業員のすべてのドキュメントを取得してから最後のインデックスを見つけます。私はここmongodbの配列の最後のインデックスを見つける

type (
     Employee struct { 
      Name    string 
      Password   string 
      EmpId    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 

     } 

は、私は自分のアプリケーションに

My code is follows 

    employee := Employee{} 
    err = c.Find(bson.M{ 
        "empid": "1234" 
       }).One(&result) 
    if err != nil { 
      log.Fatal(err) 
    } 
      name:=result.Name   
      lastindex:= result.LeaveInfo[len(result.LeaveInfo)-1].Id 
を行う方法である次のように私の構造体は、配列 の最後のインデックスを見つける前に、従業員の記録1000を取得してRAMに格納する必要があるとして、私のRAMあなたは私が全体の従業員データをretriveし、そこ最後document.Is任意のBETTのIDを見つける見ることができるように

えーthis.Appreciate任意のhelp.Please ...おかげ..

新しく追加されたコード

This code is based on your example 

    var result bson.M 

    query := bson.M{"empid": "1234"} // gets the employee you are interested in 
    match := bson.M{"$match": query} // Set up the match part of the pipeline 
    unwind := bson.M{"$unwind": "$leave"} // sets up the leave field to be unwound 

    pipeline := []bson.M{match, unwind,{ 
    "$project":bson.M{ 
      "ID":bson.M{ 
       "$slice": []interface{}{"$leave.id", -1}, 
     } 
     } 


    iter := postCollection.Pipe(pipeline).Iter() 
    for iter.Next(&result) { 
     fmt.Printf("%+v\n", result) 
    } 
    iter.Close() 

を行う方法このコードは私に同じ文書の多くを与える542件の文書のような.But、これらすべてのドキュメントが同じです...

答えて

1

$スライスを含むMongoのバージョンを実行している場合は、2.4で導入されました。これは、Select関数を追加したFindで使用できます。これは、プロジェクトのように動作します。

err = c.Find(bson.M{"empid": "1234"}).Select(bson.M{"name": 1, "leave": bson.M{"$slice": -1}}).One(&result) // result is an Employee struct 

そうでなければ、集約はあなたの友人です。パイプラインを使用し、従業員退室フィールドを巻き戻す必要があります。

は、いくつかの簡単な手順があります。

1)休職フィールドはむしろLeaveInfosのスライスよりも単一LeaveInfoとして定義され、あなたの従業員の記録に基づいて結果レコードを定義し、例えば

EmployeeResult struct { 
    Name string `bson:"name"` 
    Leave LeaveInfo `bson:"leave"` 
} 

bsonタグをEmployee構造体のLeaveInfoタグと同じ名前にすることを忘れないでください。

2)その後、ステージのカップルとパイプラインの作成:

query := bson.M{"empid": "1234"} // gets the employee you are interested in 
match := bson.M{"$match": query} // Set up the match part of the pipeline 
unwind := bson.M{"$unwind": "$leave"} // sets up the leave field to be unwound 
pipeline := []bson.M{match, unwind} // the pipeline you are passing to pipe. I like to split the parts of the pipe call up for clarity and ease of later modification 

3)をパラメータとしてパイプラインとパイプをコールし、その後イーターは結果の上に、これは一度にあなたに1つLeaveInfoを与える必要があります

var (
    result EmployeeResult 
) 
iter := postCollection.Pipe(pipeline).Iter() 
for iter.Next(&result) { 
    fmt.Printf("%+v\n", result) 
} 
iter.Close() 

4)は、ループの最後で、結果はリストの最後のアイテムを持っている、または何も読まれなかった場合は空白になります。

+0

ありがとうuser1638680 .....少なくともindex.Thisを見つけることで私たちを助けにMongoDBであり、他の属性は、間違いなく私のコードよりも優れているが、私は休暇配列の1000件のレコードが内部で取得することにしたいいけませんleave配列の2つの属性だけを取得しているにもかかわらず、リクエストごとにラム。おかげさま...もう一度あなたの助けを感謝します。 –

+0

Mongo 3.2では、$ sliceをMongoシェルの負の数値で使用できます。たとえば、 – dgm

+0

おっと、私は途中で戻ってきました。 Mongoシェルを使用すると、db.c.find({"empid": "1234"}、{"leave":{$ slice:-1}})のようなクエリを実行し、配列の最後のエントリを取得できます。私はすばやく見ましたが、Goから$ sliceを使用した例は見つかりませんでした。 – dgm

関連する問題