How to update saved document in GridFS

4,857 views
Skip to first unread message

Avise Sudhakar Rao

unread,
May 30, 2011, 4:08:00 AM5/30/11
to mongod...@googlegroups.com
Hi,

Can you please provide a sample code in Python or Java to update any existing document in the GridFS?

And also just want to know how to store Video session information in the GridFS, without storing the video file in /tmp location?

Thanks,
Sudhakar

Rajesh Koilpillai

unread,
May 30, 2011, 5:09:02 AM5/30/11
to mongod...@googlegroups.com
You will have to delete and re-create the document with the same document id (referred as _id in the metadata)
You could use the put call to store your session information in GridFS.

--
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.



--
Thanks,
- Rajesh Koilpillai

Bernie Hackett

unread,
Jun 1, 2011, 12:22:03 AM6/1/11
to mongodb-user
As Rajesh pointed out you can't update a file in GridFS, you have to
replace it by deleting the file by _id, then adding the new version
using the same _id. You can also do versioning of the file by adding
each version of the file using the same filename.

There is a simple example for using GridFS in python here:
http://api.mongodb.org/python/current/examples/gridfs.html

The api for GridFS in python is here:
http://api.mongodb.org/python/current/api/gridfs/index.html#module-gridfs

For working with multiple file versions see:
http://api.mongodb.org/python/current/api/gridfs/index.html#gridfs.GridFS.get_version
and
http://api.mongodb.org/python/current/api/gridfs/index.html#gridfs.GridFS.get_last_version

On May 30, 1:08 am, Avise Sudhakar Rao <avisesudhakar...@gmail.com>
wrote:

gaurav...@gmail.com

unread,
Jun 25, 2014, 3:08:08 AM6/25/14
to mongod...@googlegroups.com
Hello,

I recently start working on mongodb gridfs and use java spring mongodb APIs. My requirement like i want maintain versionning of file base on _id field.

Thanks & Regards
Gaurav Ashara

Stephen Steneker

unread,
Jul 2, 2014, 1:02:17 AM7/2/14
to mongod...@googlegroups.com
On Wednesday, 25 June 2014 17:08:08 UTC+10, gaurav...@gmail.com wrote:
I recently start working on mongodb gridfs and use java spring mongodb APIs. My requirement like i want maintain versionning of file base on _id field.

Hi Guarav,

Instead of replying to very old discussions, please start a new discussion topic for your question.

FYI, as per Bernie's comment on the original discussion re: GridFS, you can save multiple versions with the same filename to GridFS and they will have unique _ids assigned. The Python driver has get_version() and get_last_version() helpers for this, but you can do similar queries in any driver finding by filename and sorting by _id (or uploadDate).

Example of multiple versions of "test.osa" in the mongo shell:

db.fs.files.find({filename:'test.osa'}).sort({_id: -1})
{
 
"_id" : ObjectId("53b3902c4564b25dc2bd6f9a"),
 
"filename" : "test.osa",
 
"chunkSize" : 261120,
 
"uploadDate" : ISODate("2014-07-02T04:53:00.269Z"),
 
"md5" : "cab7cedd24b0a255f9a747d4e8dbae6c",
 
"length" : 59
}
{
 
"_id" : ObjectId("53b3902b750d22b54053c795"),
 
"filename" : "test.osa",
 
"chunkSize" : 261120,
 
"uploadDate" : ISODate("2014-07-02T04:52:59.521Z"),
 
"md5" : "cab7cedd24b0a255f9a747d4e8dbae6c",
 
"length" : 59
}

If you have further questions, let's continue on a new discussion thread :).

Thanks,
Stephen

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages