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

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog