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

查看:31
本文介绍了查询 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 与 NodeJS 结合使用,NodeJS 尚不支持 GridFS,因此我将实际的 mongodb 模块用于 GridFS 操作.似乎没有一种方法可以像查询常规集合中的文档那样查询文件元数据.

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天全站免登陆