dqpd4268 2016-08-26 08:01
浏览 458
已采纳

在MongoDB和golang(mgo)中需要在$ push中包含$ each和$ position

1.In the back end i m using go lang and for database i use mongoDB. I m trying to find the last document inserted in the embedded array so i can retrieve the document in the last array index without knowing its index.Is is possible?? After researching on this i came to know that its not possible.So i m thinking of using $push,$each and $position.Here i can set the position to 0 so the newly added document will be in 0 so i can retreive it using the index 0.

  Here is bson format
    {
    empid:"L12"
    AnnualLeave:[
        {
        "atotal" : 20,
        }
    ]
    }



 Here is my schema


 type (
            Employee struct {
                EmpId          string
                AnnualLeave    []*AnnualLeaveInfo
            }
             AnnualLeaveInfo struct {
                ATotal          int64   
            }


        I use the mgo statement as follows`enter code here`

 c.Update(bson.M{"empid": "string"}, bson.M{"$push": bson.M{"annualleave":bson.M{"$each":
         bson.M{"atotal": 4},"$position":0}}

2.Please advice me as well how to decrement the ATotal of the previous document attached and keep it as the value of the atotal of the new document. Please help me.Thanks

  • 写回答

1条回答 默认 最新

  • doudi7782 2016-10-04 06:14
    关注

    I m trying to find the last document inserted in the embedded array so i can retrieve the document in the last array index without knowing its index.Is is possible?

    You can find the last array index by derriving from the array length. Using your example:

    type Employee struct {
            EmpId string 
            AnnualLeave []AnnualLeaveInfo
    }
    type AnnualLeaveInfo struct {
            ATotal int64
    }
    
    result := Employee{}
    err = c.Find(bson.M{"empid": "example employee ID"}).One(&result)
    if err != nil {
            log.Fatal(err)
    }
    lastAnnualTotal:= result.AnnualLeave[len(result.AnnualLeave)-1].ATotal
    

    Please advice me as well how to decrement the ATotal of the previous document attached and keep it as the value of the atotal of the new document

    Depending on your use case, you could try performing two database operations:

    • Fetch the last ATotal value from the collection.
    • Push a new AnnualLeaveInfo document with the new ATotal value.
    // Assuming that EmpId is unique
    err = c.Update(bson.M{"empid": result.EmpId},
          bson.M{"$push": bson.M{"annualleave": bson.M{"atotal": int(latestAnnualTotal-1)}}})
    

    If you require atomic updates, see MongoDB Atomicity and Transactions and Model Data for Atomic Operations.

    On another note, it seems that you're trying to do something related to CQRS design patterns. This design pattern may help with calculating your annual leave use case. See also Even Sourcing with MongoDB

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改