Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

go

mgo

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?

like image 643
Bestbug Avatar asked Dec 22 '15 09:12

Bestbug


1 Answers

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

like image 160
Bestbug Avatar answered Nov 09 '22 23:11

Bestbug