Delete Olds Document from a Collection

Hi,

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

Uncaught
MongoNetworkError: connection 10 to 127.0.0.1:27017 closed

The query:
Collection: KerberosTgs_20230608031830

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

The Documents:

"_id" : ObjectId("64826f72aaaaf621c0af3357"),
"_t" : [
    "Entity",
    "Activity",
    "NetworkActivity",
    "Kerberos",
    "KerberosKdc",
    "KerberosTgs"
],
"SourceGatewaySystemProfileId" : ObjectId("6436ab3297b6be23247aa7a2"),
"HorizontalParentId" : ObjectId("000000000000000000000000"),
"StartTime" : "2023-06-09T00:16:50.4994425Z",
"EndTime" : "2023-06-09T00:16:50.5021092Z",
"SourceIpAddress" : "10.151.105.19",
"SourcePort" : 63481,

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

Thanks

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

2 Likes

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.

refs:
https://www.mongodb.com/docs/manual/faq/storage/#how-do-i-reclaim-disk-space-in-wiredtiger-
https://www.mongodb.com/docs/manual/faq/storage/#faq–mongodb-storage
https://www.mongodb.com/docs/manual/reference/command/compact/#compact
https://www.mongodb.com/docs/v4.4/tutorial/resync-replica-set-member/#resync-a-member-of-a-replica-set

2 Likes

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