How to Setup/Configure application logs Cleanup using mongo

Steps to configure Application Logs Cleanup using MongoDB

Step 1:

Please Find Attached js(extn is .js) file and place the file in C:\MongoDB\4.2\bin folder.

.Jsfile, batchfile

Step 2: Edit File

use <DBName>:-- Database name on which we have to perform operation

db.logmessageentity.remove({ CreatedDate:{   $lte : new Date(ISODate().getTime() - 1000 * 86400 * 60)} });--replace 60 with the no. of days of which you want to delete logs

db.runCommand({compact:'logmessageentity'});

Step 3 :

Please find the Attached batch (extn is .bat) file and place the same in any drive available on a machine like c:\ or d:\

Step 4:

Edit the batch file :

echo Starting log collection clearing...

cd C:\MongoDB\4.2\bin\ --please give the proper path where the js file has been placed

mongo <deleteLogMessagesLogs.js

echo log collection removed.

Step 5:

Setup done now we can Cleanup logs by executing(Simply double click on the batch file) the batch file or we can schedule the activity using windows task scheduler.

Steps to Schedule tasks in Windows task Scheduler.

Step 1:Go to task scheduler click on create task then click on below mentioned check boxes,

The User must be administrators.


Step 2:


Step 3: Go to trigger schedule the task accordingly as per the requirement.



Step 4: Go to actions browse batch file



Step 5:

Task is scheduled, click on Ok.

Mongo 6.0 Version : required MongoSH for Command Shell

From MongoDB 5 Version, Command Shell is deprecated, to Execute the js files we need to install MongoSH explicitly. 

URL : 

https://downloads.mongodb.com/compass/mongosh-2.2.12-win32-x64.zip


Move JS file to the MongoSH bin folder and reconfigure the scheduler accordingly


Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.