doujia4619 2015-12-22 01:30 采纳率: 0%
浏览 289
已采纳

使用mgo在mongo中使用_id删除所有早于日期的文档

I'm working in Golang and mgo and I would like to delete all the documents in a collection older than a specified date, using _id value.

So far I've tried to create a dummy objectId using a struct NewObjectIdWithTime after that I'm trying to delete documents using

collection.Remove(bson.M{"_id": bson.M{"$lt": objectId}})

But I'm not getting any results, any suggestion?

  • 写回答

1条回答 默认 最新

  • douyi1944 2015-12-22 02:49
    关注

    I really don't like answer my self but since the only help I recive from stackoverflow community was a negative rating (without any explain) I post the solution:

    The problem is mgo have RemoveAll where delete all the element match the criteria, so my new query is:collection.RemoveAll(bson.M{"_id": bson.M{"$lt": objectId}})

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部