英文:
Delete all the document older than a date using _id in mongo using mgo
问题
我正在使用Golang
和mgo
工作,并且我想通过_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}})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论