Dewfy
Dewfy

Reputation: 23644

server side Mongodb GridFS file copying

Per business requirements I need provide possibility to copy content of some file on GridFS. Of course it can be done over domain-specific layer. But in this case I can see some overhead:

Obvious solution is write mongo-side JavaScript that will perform copying in bound of single server. So my questions:

Thank you in advance

Upvotes: 3

Views: 2463

Answers (3)

Alex Isayko
Alex Isayko

Reputation: 322

gridfs_session = gridfs.GridFS(mongo.session)
out_file = gridfs_session.get(file_id)
new_copy = gridfs_session.put(out_file, content_type=out_file.content_type)

Upvotes: 0

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24007

You never need to copy a GridFS file within a single server, because GridFS files are immutable: you can create, read, or delete them, but not modify them. So there's no reason to make a copy.

Copying from one server to another should be done via a driver; there's no built-in support for copying directly from a MongoDB server to another.

Upvotes: 3

Frederick Roth
Frederick Roth

Reputation: 2830

The 'normal' js driver does not support GridFS.

You could do it in Node.js. The documentation is here:

http://mongodb.github.com/node-mongodb-native/markdown-docs/gridfs.html

For replica-sets the documentation can be found here for Node.js:

http://mongodb.github.com/node-mongodb-native/markdown-docs/replicaset.html

For simple one-time copying of some files you can use mongofiles on command line (using a temporary file :( ) :

http://www.mongodb.org/display/DOCS/GridFS+Tools

mongofiles --host HOST get currentfilename
mongofiles --host HOST put -l currentfilename newfilename
rm currentfilename

I however don't know how well mongofiles works with sharding and replicas but I would expect it to work.

Upvotes: 0

Related Questions