查询MongoDB GridFS? [英] Querying MongoDB GridFS?

查看:335
本文介绍了查询MongoDB GridFS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个博客系统,可以将上传的文件存储到GridFS系统中.问题是,我不知道如何查询它!

I have a blogging system that stores uploaded files into the GridFS system. Problem is, I dont understand how to query it!

我将Mongoose与尚未支持GridFS的NodeJS一起使用,因此我在GridFS操作中使用了实际的mongodb模块.没有SEEM可以像常规集合中的文档一样查询文件元数据.

I am using Mongoose with NodeJS which doesnt yet support GridFS so I am using the actual mongodb module for the GridFS operations. There doesn't SEEM to be a way to query the files metadata like you do documents in a regular collection.

将元数据存储在指向GridFS objectId的文档中是否明智?以便能够轻松查询?

Would it be wise to store the metadata in a document pointing to the GridFS objectId? to easily be able to query?

任何帮助将不胜感激,有点卡住:/

Any help would be GREATLY appreciated, im kinda stuck :/

推荐答案

GridFS 可行通过为每个文件存储许多块.这样,您可以交付和存储非常大的文件,而不必将整个文件存储在RAM中.此外,这使您可以存储大于最大文档大小的文件.建议的块大小为256kb.

GridFS works by storing a number of chunks for each file. This way, you can deliver and store very large files without having to store the entire file in RAM. Also, this enables you to store files that are larger than the maximum document size. The recommended chunk size is 256kb.

文件元数据字段可用于存储其他特定于文件的元数据,这比将元数据存储在单独的文档中更为有效.这在很大程度上取决于您的确切要求,但是通常,元数据字段提供了很大的灵活性.请记住,默认情况下,某些更明显的元数据已经是fs.files文档的一部分:

The file metadata field can be used to store additional file-specific metadata, which can be more efficient than storing the metadata in a separate document. This greatly depends on your exact requirements, but the metadata field, in general, offers a lot of flexibility. Keep in mind that some of the more obvious metadata is already part of the fs.files document, by default:

> db.fs.files.findOne();
{
    "_id" : ObjectId("4f9d4172b2ceac15506445e1"),
    "filename" : "2e117dc7f5ba434c90be29c767426c29",
    "length" : 486912,
    "chunkSize" : 262144,
    "uploadDate" : ISODate("2011-10-18T09:05:54.851Z"),
    "md5" : "4f31970165766913fdece5417f7fa4a8",
    "contentType" : "application/pdf"
}

要从GridFS中实际读取文件,您必须从fs.files获取文件文档,并从fs.chunks获取块.最有效的方法是逐块将其流式传输到客户端,因此您不必将整个文件加载到RAM中. chunks集合具有以下结构:

To actually read the file from GridFS you'll have to fetch the file document from fs.files and the chunks from fs.chunks. The most efficient way to do that is to stream this to the client chunk-by-chunk, so you don't have to load the entire file in RAM. The chunks collection has the following structure:

> db.fs.chunks.findOne({}, {"data" :0});
{
    "_id" : ObjectId("4e9d4172b2ceac15506445e1"),
    "files_id" : ObjectId("4f9d4172b2ceac15506445e1"),
    "n" : 0, // this is the 0th chunk of the file
    "data" : /* loads of data */
}

如果要使用fs.filesmetadata字段进行查询,请确保您了解点符号,例如

If you want to use the metadata field of fs.files for your queries, make sure you understand the dot notation, e.g.

> db.fs.files.find({"metadata.OwnerId": new ObjectId("..."), 
                    "metadata.ImageWidth" : 280});

还要确保您的查询可以使用 explain() 使用索引.

also make sure your queries can use an index using explain().

这篇关于查询MongoDB GridFS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆