3

我有一个使用 MongoDB + GridFS 的电子商务网站。每个产品最多可以有 5 张图片。每个图像有 3 个不同大小的缩略图。

为此,我需要有关最佳数据库结构的建议。

目前我正在考虑在每个产品中存储图像 ID 和拇指 ID(来自 GridFS 的 ID):

{
   '_id': 1, 
   'title': 'Some Product', 
   'images': [
               {'id': '11', thumbs: {'small': '22', 'medium': '33'},
               {'id': '44', thumbs: {'small': '55', 'medium': '66'}  
             ]
}

或者将路径存储在GridFS中会更好吗?

{
  '_id': '111',
  'filename': '1.jpg',
  'path': 'product/988/image/111/'
},
{
  '_id': '222',
  'filename': '1.jpg',
  'path': 'product/988/image/111/thumbnail_small'
},
{
  '_id': '333',
  'filename': '1.jpg',
  'path': 'product/988/image/111/thumbnail_large'
}

更新:GridFS 中的“路径”字段是“假”路径,而不是真实路径。只是一种快速查找所有相关文件的方法。拥有 1 个索引字段比具有复合索引的多个字段便宜。

4

1 回答 1

4

如果您要在 MongoDB 中使用 GridFS 存储图像,我会选择第一个。

The second schema doesn't seem to be correct. I mean GridFS is supposed to store files, so with the id of the image you don't need any path within those documents. If you simply want to store the path of the file, directly embedd it into your primary collection, so you don't need this overhead of a somewhat useless collection.

In general see Storing Images in DB - Yea or Nay? if you really should store your images in dbms.

Additionally if you only save the path you may need few to no changes in case you're switching to some CDN to get your images to the customer.

于 2012-05-11T13:15:48.693 回答