gc5
gc5

Reputation: 9868

How to save via mongodb shell a file served by GridFS in a mongoengine/Django FileField

I have a file saved on GridFS, via mongofiles

$ mongofiles -d dba put file.txt
connected to: 127.0.0.1
added file: { _id: ObjectId('50c0a48f8d1d53325a7d7b01'), filename: "file.txt", chunkSize: 262144, uploadDate: new Date(1354802319244), md5: "a6039c33d297d12d0dfb9b1a99bda542", length: 3527 }
done!
$ mongofiles -d dba list
connected to: 127.0.0.1
file.txt    3527

I have a model for Django & mongoengine that is defined as

class IFile(Document):
    iptr = FileField(required=True)

How can I save from mongodb shell this file inside an instance of IFile?

db.ifile.save({"iptr" : ??})

Thanks

Upvotes: 0

Views: 1353

Answers (1)

Crazyshezy
Crazyshezy

Reputation: 1620

Internally GridFS uses two collections to store data:

  • fs.files contains the object metadata
  • fs.chunks contains the binary chunks with some additional accounting information

Files

Documents in the files collection require the following fields:


{
  "_id" : ,            // unique ID for this file
  "length" : data_number,           // size of the file in bytes
  "chunkSize" : data_number,        // size of each of the chunks.  Default is 256k
  "uploadDate" : data_date,         // date when object first stored
  "md5" : data_string               // result of running the "filemd5" command on this file's chunks
}

Chunks

The structure of documents from the chunks collection is as follows:


{
  "_id" : ,         // object id of the chunk in the _chunks collection
  "files_id" : ,    // _id of the corresponding files collection entry
  "n" : chunk_number,            // chunks are numbered in order, starting with 0
  "data" : data_binary,          // the chunk's payload as a BSON binary type
}

For more detailed information, click here Related Driver docs: Python

Upvotes: 2

Related Questions