适用于网络照相馆的NoSQL数据架构 [英] Proper NoSQL data schema for web photo gallery

查看:95
本文介绍了适用于网络照相馆的NoSQL数据架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种用于NoSQL存储照片库的适当数据结构。在我的Web应用程序中,一张照片可以属于1张或多张相册。我对MySQL有经验,但对键值存储却几乎没有经验。

I'm looking to build an appropriate data structure for NoSQL storage of a photo gallery. In my web application, a photo can be part of 1 or more albums. I have experience with MySQL, but almost none with key-value storage.

对于MySQL,我将按照以下步骤设置(3)张表:

With MySQL, I would have set up (3) tables as follows:

photos (photo_id, title, date_uploaded, filename)
albums (album_id, title, photo_id)
album_photo_map (photo_id, album_id)

然后,要检索最近5张照片的列表(带有相册数据),请查询像这样:

And then, to retrieve a list of the 5 latest photos (with album data), a query like this:

SELECT *
FROM albums, photos, album_photo_map
WHERE albums.album_id = album_photo_map.album_id AND
                photos.photo_id = album_photo_map.photo_id
ORDER BY photos.date_uploaded DESC LIMIT 5;

如何使用NoSQL键值对数据库完成类似的查询? (特别是Amazon的DynamoDB。)存储的外观如何?

How would I accomplish a similar query using a NoSQL key-value pair database? (Specifically, Amazon's DynamoDB.) What would the storage look like? How would the indexing work?

推荐答案

使用mongodb lingo,您的集合可能如下所示:

Using mongodb lingo, your collections could look like this:

photos = [
    {
        _id: ObjectId(...),
        title: "...",
        date_uploaded: Date(...),
        albums: [
            ObjectId(...),
            ...
        ]
    },
    ...
]

albums = [
    {
        _id: ObjectId(...),
        title: "..."
    }
]

查找5张最新照片的方法如下:

Finding the 5 newest photos would be done like this:

> var latest = db.photos.find({}).sort({date_uploaded:1}).limit(5);

mongo中没有服务器端连接,因此您必须获取所有最新专辑,例如

There's no server-side joins in mongo, so you'd have to fetch all the latest albums like this:

> var latest_albums = latest.find({}, {albums: 1});

当然,那么您必须将其煮成一组。

Of course, then you have to boil this down into a set.

如果只是将相册嵌入照片文档中,则实际上会更容易,因为它们很小:

It's actually easier if you just embed the album inside the photo documents, since they're small:

photos = [
    {
        _id: ObjectId(...),
        title: "...",
        date_uploaded: Date(...),
        albums: [
            {name: "family-vacation-2011", title: "My family vacation in 2010"},
            ...
        ]
    },
    ...
]

然后查询是一样的,但是您不会不必参加。查找相册中的所有照片如下:

Then querying is the same, but you don't have to join. Finding all photos in an album looks like:

> db.photos.find({albums:{$elemMatch:{name: "family-vacation-2011"}}});

这篇关于适用于网络照相馆的NoSQL数据架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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