DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Keep Your Mongo Log Database Manageable by Deleting Old Logs

Keep Your Mongo Log Database Manageable by Deleting Old Logs

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
Apr. 04, 12 · Interview
Like (0)
Save
Tweet
Share
5.49K Views

Join the DZone community and get the full member experience.

Join For Free

As I showed in an old post, Mongo Db is perfect to store logs, but as everyone knows, log databases tend to become really big, especially if the verbosity level is high, so I usually schedule a process that deletes all log older than a certain date to free space in log databases. To cleanup record in a Mongo Db I decided to create a little Powershell script to delete all entry in a collection that are older than a certain value. The whole scritp is reported here.

write-host "Cleanup Mongo Db"
$now = Get-Date
$limit = $now.AddDays(-20)
write-host "Date: " + $limit.Year
write-host "Date: " + $limit.Month
write-host "Date: " + $limit.Day
 
$si = new-object System.Diagnostics.ProcessStartInfo
$si.FileName = "Mongo.exe"
$si.WorkingDirectory = "c:MongoBin"
$si.Arguments = "localhost:27017/log4Net --eval ""db.mylogtable.remove({timestamp: {`$lt:new Date(" +
$limit.Year + ", " + ($limit.Month - 1).ToString() + ", " + $limit.Day + ")}})"""
 
$p = [diagnostics.process]::Start($si)

The scritp is really simple first of all I get the actual date with the Get-Date powershell function, then I subtract 20 days to create the date limit and grab the year, month and day part of it The cleanup process is a simple call to Mongo.exe command line tool that accepts the address of the database as first argument in the form of address:port/databasename followed by –eval to directly execute a command. The real delete command has this form: db.NameOfTheCollection.Remove(Condition), in this situation the condition is something like {timestamp: {$lt:new Date(year, month, day)}}. Pay attention to the thick sign (`) before the $lt, because it is the escape character used in powershell and it is needed to insert a dollar sign inside a string.

All you need to do is schedule this script to run once a day to keep in your Mongo database only the last 20 days of logs.

Database

Published at DZone with permission of Ricci Gian Maria. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • Understanding and Solving the AWS Lambda Cold Start Problem
  • Introduction to Automation Testing Strategies for Microservices
  • Building the Next-Generation Data Lakehouse: 10X Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: