需要在 MongoDB 和 golang(mgo) 中包含 $each 和 $position with $push
Need to include $each and $position with $push in MongoDB and golang(mgo)
1.In 后端我使用 go lang,数据库我使用 mongoDB。我试图找到嵌入数组中插入的最后一个文档,这样我就可以在不知道它的 index.Is 可能的情况下检索最后一个数组索引中的文档?
在对此进行研究之后,我开始知道它不是 possible.So 我正在考虑使用 $push、$each 和 $position.Here 我可以将位置设置为 0 这样新添加的文档将在 0 中所以我可以使用索引 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 也请教我如何减少附加的上一个文档的 ATotal 并将其保留为新文档的 atotal 值。
请帮忙me.Thanks
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?
您可以通过数组长度推导出最后一个数组索引。使用您的示例:
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
根据您的用例,您可以尝试执行两个数据库操作:
- 从集合中获取最后一个
ATotal
值。
- 使用新的
ATotal
值推送一个新的 AnnualLeaveInfo
文档。
// 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)}}})
如果您需要原子更新,请参阅 MongoDB Atomicity and Transactions and Model Data for Atomic Operations。
另外,您似乎正在尝试做一些与 CQRS design patterns. This design pattern may help with calculating your annual leave use case. See also Even Sourcing with MongoDB
相关的事情
1.In 后端我使用 go lang,数据库我使用 mongoDB。我试图找到嵌入数组中插入的最后一个文档,这样我就可以在不知道它的 index.Is 可能的情况下检索最后一个数组索引中的文档? 在对此进行研究之后,我开始知道它不是 possible.So 我正在考虑使用 $push、$each 和 $position.Here 我可以将位置设置为 0 这样新添加的文档将在 0 中所以我可以使用索引 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 也请教我如何减少附加的上一个文档的 ATotal 并将其保留为新文档的 atotal 值。 请帮忙me.Thanks
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?
您可以通过数组长度推导出最后一个数组索引。使用您的示例:
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
根据您的用例,您可以尝试执行两个数据库操作:
- 从集合中获取最后一个
ATotal
值。 - 使用新的
ATotal
值推送一个新的AnnualLeaveInfo
文档。
// 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)}}})
如果您需要原子更新,请参阅 MongoDB Atomicity and Transactions and Model Data for Atomic Operations。
另外,您似乎正在尝试做一些与 CQRS design patterns. This design pattern may help with calculating your annual leave use case. See also Even Sourcing with MongoDB
相关的事情