如何使用golang获取mongodb中数组中的最后一个元素?
I'm working in go language. I'm using mgo driver to get the data from mongodb. I want last entry from the events array.
There will be huge data in future. so I don't want to read the whole record but just want specific data from the record.
db.events.find({"_id":"59ce53b9-970a-44a2-8419-b41a99120b25"},{"events":{$slice:-1}}).pretty()
this is working in mongo shell. I want this to work in go lang.
This is the sample data, from which I want last entry present in events.
"_id" : "59ce53b9-970a-44a2-8419-b41a99120b25",
"version" : 9,
"events" : [
"event_type" : "customer:added",
"data" : {
"id" : "59ce53b9-970a-44a2-8419-b41a99120b25",
"name" : "arjun"
"timestamp" : ISODate("2017-11-20T12:21:34.910Z"),
"aggregate_type" : "customer",
"_id" : "59ce53b9-970a-44a2-8419-b41a99120b25",
"version" : 1
"event_type" : "customer:address-updated",
"data" : {
"id" : "59ce53b9-970a-44a2-8419-b41a99120b25",
"address" : "bangalore"
"timestamp" : ISODate("2017-11-20T12:22:08.496Z"),
"aggregate_type" : "customer",
"_id" : "59ce53b9-970a-44a2-8419-b41a99120b25",
"version" : 2
0 提建议
邀请回答
编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金
¥ 0
(可追加 ¥500)
支付方式
余额支付
余额: ¥ 499
扫码支付
提供问题酬金的用户不参与问题酬金结算和分配
支付即为同意
《付费问题酬金结算规则》
1
条回答
默认
最新
查看更多回答(-1条)
报告相同问题?
相关推荐
更多相似问题