如何通过mongodb shell在mongoengine / Django FileField中保存由GridFS提供的文件

时间:2012-12-06 14:22:25

标签: django mongodb mongoengine gridfs filefield

我通过mongofiles

在GridFS上保存了一个文件
$ 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

我有一个Django& amp;模型mongoengine,定义为

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

如何从mongodb shell中将此文件保存在IFile的实例中?

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

由于

1 个答案:

答案 0 :(得分:2)

内部GridFS使用两个集合来存储数据:

  • fs.files包含对象元数据
  • fs.chunks包含带有一些额外会计信息的二进制块

文件

文件集合中的文档需要以下字段:


{
  "_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
}

结块

来自chunk集合的文档结构如下:


{
  "_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
}

有关详情,请点击here 相关的驱动程序文档:Python