Delete Olds Document from a Collection


I want to delete older documents from a collection but always fails

MongoNetworkError: connection 10 to closed

The query:
Collection: KerberosTgs_20230608031830

db.KerberosTgs_20230608031830.deleteMany( { StartTime : {“$lt” : new Date(2023, 11, 1) }

The Documents:

"_id" : ObjectId("64826f72aaaaf621c0af3357"),
"_t" : [
"SourceGatewaySystemProfileId" : ObjectId("6436ab3297b6be23247aa7a2"),
"HorizontalParentId" : ObjectId("000000000000000000000000"),
"StartTime" : "2023-06-09T00:16:50.4994425Z",
"EndTime" : "2023-06-09T00:16:50.5021092Z",
"SourceIpAddress" : "",
"SourcePort" : 63481,

Can you please help on this issue? I just want to delete six months’ older documents from the collection.


Hi @Kerem

The date type of StartTime is string so comparing with a date object in this case wont work.

Fortunately the string is an ISODate so the lexicographical order is equal to the chronological order.
So taking the ISOString from the date object will work:
db.KerberosTgs_20230608031830.deleteMany( { StartTime : {"$lt" : (new Date(2023, 11, 1)).toISOString() }

Or use a string directly:
db.KerberosTgs_20230608031830.deleteMany( { StartTime : {"$lt" : "2023-11-01T00:00:00.000Z" }})


Hi @chris

Thank you so much for your efforts and for helping me understand. The first query is not working but the second query works perfectly.

I just wonder

Why the second query doesn’t work?

Why didn’t the disk size change?

Thank you again

The first one? could be syntax. Try assigning the value to variable first, and make sure the string is correct.

var deleteBefore = new Date(2023,11,1)
var dateString = deleteBefore.toISOString()
// inspect var

db.KerberosTgs_20230608031830.deleteMany( { StartTime : {"$lt" : dateString}})

Deleting documents will not free space back to the OS. The space created by document removal will be reused by the collection for future inserts and updates.

A collection can be compacted to reclaim the freed blocks. An alternative is to resync replicaset members this has the side effect of ‘defragmenting’ all collections.



This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.