[JAVA-1205] GridFs Created: 08/May/14  Updated: 08/May/14  Resolved: 08/May/14

Status: Closed
Project: Java Driver
Component/s: GridFS
Affects Version/s: 2.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Ilario Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS X Maverick



 Description   

I am using Gridfs for storing a big file in mongodb. This is my code to insert the file:

GridFS bigFile = new GridFS(db, "35");
File f = new File(path+"/element.son");
GridFSInputFile gfsFile = bigFile.createFile(f);
gfsFile.setFilename("35");
gfsFile.save();

The file was successfully inserted intact, if I try to show it through the restful interface I had the follow response:

{
 "offset" : 0,
 "rows": [
    { "_id" : { "$oid" : "536ad33c300433edfe512724" }, "chunkSize" : 261120, "length" :             
    17647218, "md5" : "a86b60fb2d219d26b37256c89d1bb7f2", "filename" : "35", "contentType" :   
    null, "uploadDate" : { "$date" : 1399509820201 }, "aliases" : null }
  ],
  "total_rows" : 1 ,
  "query" : {} ,
  "millis" : 0
 }

Now, I want to delete this bucket, this is my code:

GridFS gridFs = new GridFS(db, "35");
GridFSDBFile fi = gridFs.findOne("35");
GridFSDBFile file = gridFs.findOne(new ObjectId(fi.getId().toString()));
gridFs.remove(file);

Apparently the file has been deleted infact the response is:

{
 "offset" : 0,
 "rows": [], 
 "total_rows" : 0 ,
 "query" : {} ,
 "millis" : 0
}

but, if I show the collections in my console I have still my bucket (35.files,35.chunks).

Why after the remove in the console I have still this bucket? is it a bug or I haven't understood some stuff?



 Comments   
Comment by Ross Lawley [ 08/May/14 ]

That is to be expected, MongoDB aggressively preallocates data files to reserve space and avoid file system fragmentation. To free up that space you'd have to drop the database - that would also remove any other collections!

The storage documentation explains in more detail about MongoDB's storage engine. Also the smallFiles configuration option may be of interest as well.

Ross

Comment by Ilario [ 08/May/14 ]

I mean, if I have only one database (test) with only one Gridfs file (35.files and 35.chunks) the dimension of the db (show dbs) is 0.20 Gb.
After the deletion of the GridFs file the size of the db is the same.

Comment by Ross Lawley [ 08/May/14 ]

ilamaiolo If that answers your concerns, I'll close this ticket.

Comment by Ross Lawley [ 08/May/14 ]

When you say dimension - what do you mean? If you mean the amount of space the database takes up on disk then that is as expected, but the space is on the free list and as the database grows will be used by other collections / documents.

For more information about storage see the storage documentation

For future, the best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a boarder audience there.

Ross

Comment by Ilario [ 08/May/14 ]

Yes, you have been very clear.
I have another doubt, you said that the statement "gridFs.remove(ObjectId) in the background it simply removes all matching documents", but after this statement the dimension of my db should be decrease, but the dimension is the same.
Is it normal?
This kind of behavior is for the same reason that you have mentioned?

Thanks in advance for your help.

Comment by Ross Lawley [ 08/May/14 ]

Hi,

GridFs is a convention build upon MongoDB and exists solely in the driver code. It helps facilitate the storing and retrieving files that maybe bigger that the 16MB document limit.

When calling gridFs.remove(ObjectId) in the background it simply removes all matching documents and as such doesn't drop any collections, which is why you are still seeing them in the console. You have to manually drop the collections if they are no longer required.

I should also mention GridFS can also store multiple versions of a file. For example if you create a file with the name "35" and then later create a new file "35", it won't overwrite the original file but instead stores the new file as well. gridFs.remove(ObjectId) will only remove one version of that file (the one with the matching _id) but if you wanted to totally remove the file then calling: gridFs.remove(filename) is the way to do that.

I hope that helps clear it up!

Ross

Generated at Thu Feb 08 08:54:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.