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

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

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

我真的不喜欢自己回答,但由于我从 Whosebug 社区获得的唯一帮助是负面评价(没有任何解释)我 post 解决方案:

问题是 mgo RemoveAll 删除所有符合条件的元素,所以我的新查询是:collection.RemoveAll(bson.M{"_id": bson.M{"$lt": objectId}})