Michael Scharhag

About Michael Scharhag

Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.

MongoDB: GridFS remove method deletes all files in bucket

Some time ago we ran into strange behaviour of MongoDB’s GridFS which caused me creating a Bug Ticket for the MongoDB Java driver.

Today I found the link to the bug ticket in my browser bookmarks. The ticket isn’t solved at the current time so I thought it would be worth a short blog post in case someone else runs into this problem.

Let’s look at the following simplified Java service:
 
 
 

public class GridFsService {

  private GridFS gridFs;

  public void connect(String mongoDbHost, String databaseName) throws UnknownHostException {
    DB db = Mongo.connect(new DBAddress(mongoDbHost, databaseName));
    this.gridFs = new GridFS(db, "myBucket");
  }

  public void removeGridFsFile(String id) {
    GridFSDBFile file = this.gridFs.findOne(new ObjectId(id));
    this.gridFs.remove(file);
  }

  // .. other methods to create and update files
}

This service uses the MongoDB Java driver to create, update and remove files from GridFS. However, there is a serious flaw in the removeGridFsFile() method. Guess what happens if an invalid id is passed to removeGridFsFile(). gridFs.findOne() returns null for non existent ids. So null is passed to gridFs.remove() which then removes all files in the current bucket.

Fixing this is easy. Just add a null check or use another GridFS remove() method that takes an ObjectId instead of a GridFsDBFile:

public void removeGridFsFile(String id) {
  this.gridFs.remove(new ObjectId(id));
}

Using this way everything works fine if an invalid id is passed to removeGridFsFile() (no file is removed). To make sure this won’t happen again I tested what happens if null is passed to any of the three different remove() methods:

gridFs.remove((String)null);      // nothing happens
gridFs.remove((ObjectId)null);    // nothing happens
gridFs.remove((DBObject)null);    // all files from bucket are removed

I don’t know if this is intended behaviour. The Javadoc comment for gridFs.remove(DBObject query) tells me that it removes all files matching the given query. However, if it is intended I think it should be clearly stated in the javadoc comment that passing null removes all files in the bucket.
 

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "MongoDB: GridFS remove method deletes all files in bucket"

  1. dharshanr says:

    Thanks for the tips. GridFS is a very useful abstraction on top of MongodB. http://blog.mongodirector.com/when-to-use-gridfs/

Leave a Reply


6 + one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books