DB Script To Clear Logging From Mongo DB

From Znode Knowledge Base
Revision as of 07:14, 26 August 2019 by KB (talk | contribs) (Created page with "====='''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 cl...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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()

Script:

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.