使用_mgo在Mongo中删除所有早于某个日期的文档。

huangapple go评论90阅读模式
英文:

Delete all the document older than a date using _id in mongo using mgo

问题

我正在使用Golangmgo工作,并且我想通过_id值删除集合中早于指定日期的所有文档。

到目前为止,我尝试使用结构体NewObjectIdWithTime创建一个虚拟的objectId,然后尝试使用以下代码删除文档:

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

但是我没有得到任何结果,有什么建议吗?

英文:

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

得分: 8

我真的不喜欢回答自己的问题,但由于我从stackoverflow社区得到的唯一帮助是一个负面评分(没有任何解释),所以我发布了解决方案:

问题在于mgo有一个RemoveAll方法,可以删除所有与条件匹配的元素,所以我的新查询是:collection.RemoveAll(bson.M{"_id": bson.M{"$lt": objectId}})

英文:

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}})

huangapple
  • 本文由 发表于 2015年12月22日 17:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/34412502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定