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 关于模型训练的一个问题
  • ¥15 装了几千台服务器从来没遇到这种问题,哎看下哪位帮我解决吧
  • ¥15 单片机程序上的困难问题
  • ¥15 请教某软件缓存Ts文件破解合并mp4的方法
  • ¥15 求小游戏炸弹人中关于敌人的C++代码
  • ¥15 拿到这个服务器最高权限有偿
  • ¥50 来个抓app跳转支付宝转链接的
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错