Re: [mongodb-user] How can i update file's data in GridFS.?

2,799 views
Skip to first unread message

Nat

unread,
Jun 5, 2011, 9:42:12 AM6/5/11
to mongod...@googlegroups.com
With gridfs, You can only put in a new file.
-----Original Message-----
From: Wachirawut Thamviset <twac...@gmail.com>
Sender: mongod...@googlegroups.com
Date: Sun, 5 Jun 2011 00:27:57
To: mongodb-user<mongod...@googlegroups.com>
Reply-To: mongod...@googlegroups.com
Subject: [mongodb-user] How can i update file's data in GridFS.?

First, I insert file "photo.jpg" to GridFS
// ---- example code ----
GridFS gfs = new GridFS(db);
GridFSInputFile f = gfs.createFile(new File("photo.jpg"));
f.save();


So, I can retrive that file by:

GridFS gfs = new GridFS(db);
GridFSDBFile f = gfs.findOne("photo.jpg");


But, if I have new file of "photo.jpg". How can I update (replace)
an old file.?

If I use this code again:

GridFS gfs = new GridFS(db);
GridFSInputFile f = gfs.createFile(new File("photo.jpg"));
f.save();

It will be a new record.


Thank.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Wachirawut Thamviset

unread,
Jun 5, 2011, 3:27:57 AM6/5/11
to mongodb-user

Scott Hernandez

unread,
Jun 5, 2011, 11:35:27 AM6/5/11
to mongod...@googlegroups.com
When you add a new one it doesn't remove the old one (and replace is not a safe operation); some drivers have a helper method to do this.

Generally you want to follow this pattern:

1.) find old one, keep the _id
2.) add new one
3.) remove old one by _id

Dirk Schumacher

unread,
Oct 7, 2014, 11:42:59 PM10/7/14
to mongod...@googlegroups.com
Hello,

ok this post is about 3 years old know.
The Ticket https://jira.spring.io/browse/DATAMONGO-625
referenced by http://stackoverflow.com/questions/15365476/replace-an-existing-gridfs-file-with-new-content-using-spring-data-mongodb
is still open.

I was also trying to find the how to do it. My point of view of the save operation is basically saving an artefact under the given parameters.
If replacing would not be wanted that would need an extra check instead vice versa that saving fails because it already was.
If the operation was called create in terms of an 'CRUD' operation I would say yes failing would be correct because update would be the proper operation.
But the API is what the API is though the documentation on Java's com.mongodb.gridfs.GridFSInputFile is not clear about it.

Plus, I do not agree (or understand the ) of Scott's pattern. You would still have to use a new ID.
My suggestion, which is also not able to recoverable, due to that fact that mongo driver itself does not provide the atomic operation:

do everything synchronized on the ID
1. get file by ID, store it for recovery
2a. remove file by ID; if fails
2b. if 2a fails throw exception/return error
3a. add new file by ID;
3b. if 3a fails try to store old file by ID and return error
3c. if 3b fails throw LostDataException
4. return success
Reply all
Reply to author
Forward
0 new messages