相关文章推荐
慷慨大方的薯片  ·  Python File ...·  1 年前    · 
玩篮球的火锅  ·  How to get class name ...·  1 年前    · 
玩命的脸盆  ·  Degraded PnP class ...·  1 年前    · 
爱看书的熊猫  ·  MySQL ...·  1 年前    · 
dougu1990 2017-11-24 09:37
浏览 1582
已采纳

如何使用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

1 条回答 默认 最新

查看更多回答(-1条) 报告相同问题?

悬赏问题

  • ¥15 C++中使用mlpack 西门子S7-200 实现步进电机暂停功能 为什么我安装Linux的时候一直卡在下面这个配置界面 Power Automate Desktop运行SAP脚本提示: 语法错误 微信小程序可以将数组读出来,但是不能显示出来,而且appdata中可以查到数组内容 基于51单片机智能鱼缸控制系统设计 DSP在线调试出现故障 Stata软件Probit估计结果的解读