DB Script To Clear Logging From Mongo DB

From Znode Knowledge Base
Jump to: navigation, search
Problem Statement

Earlier, LogMessages were not being deleted from the collection, due to which database not used to perform well. To resolve this issue, a cleanup script was needed.

Mongo Command- db.collection.remove() :

The db.collection.remove() method is used to delete collection data from collection.

The syntax for this command is as follows: db.collection.remove()


A batch file is provided which when executes deletes logmessageentity collection data from Mongo DB whose name is provided in the batch file connection string:-

cd \Program Files\MongoDB\Server\3.2\bin set /p Count= Enter the no. of days since when the logging should be removed: mongo.exe AC-SVR024:27017/Znode_9_0_0_Custom_LogMessage --eval "db.logmessageentity.remove({ CreatedDate:{ $lte : new Date(ISODate().getTime() - 1000 * 86400 * "%Count%")} });" pause

Steps for executing batch file :
  1. Edit the batch file and modify the connection string.
  2. Modify server name and Mongo DB name.
  3. Provide the number of days from which the log should be cleared.
  4. Save the batch file and execute it.
  5. The logmessageentity collection data generated X days ago will be cleared. Here X represents the number of days provided.