1

我正在解决这个问题:

我正在构建一个 IMGUR 克隆,用户可以在其中上传图像,并且有一个“最新上传”页面,显示最近上传的 1000 张图像。

  • 用户一注册就可以上传图片,但是
  • 在用户验证他们的电子邮件地址之前,他们的上传不会显示在“最新上传”中
  • 一旦用户验证了他们的电子邮件,他们的图像就会开始出现。
  • 如果用户被禁止,他们的图片不会显示在“最新上传”中

最初我的图像包含一个用户引用,我会选择最后 1000 个填充用户的图像。然后,我将遍历返回的集合,丢弃被禁止或未经验证的用户拥有的图像。当最后 1000 张图片由未经验证的用户上传时,这种情况就被打破了。

我正在考虑在 User 对象上使用一组内部 Image 文档,但这也不理想,因为 User 可能拥有很多图像,而且我在加载 User 对象时并不总是想加载它们。

我愿意接受任何解决方案

4

1 回答 1

1

I would do the following based on what knowledge I have of your application:

There are two entities that should exist in two different collections: user and uploads.

The uploads collection will be very large, so we want to make sure we can index and shard the collection to handle the scale and performance required by your queries. With that said, some key elements in uploads are:

uploads=
{
_id:uploadId
user:{id:userId, emailverified:true, banned:false}
ts:uploadTime
.
.
.
}

possible indexes:

i. {ts:1,banned:1,"user.emailverified":1,"user.banned":1} (this index should be multi-purpose)
ii. {"user.id":1,ts:1}

Note that I store some redundant data to optimize your latest 1000 query. The cost is that in the rare case where emailverified and banned have to be updated, you need to run an update on your user collection as well as your uploads collection (this one will require multi:true).

query:

db.uploads.find({ts:{$gt:sometime},banned:false,emailverified:true}.sort({ts:-1}).limit(1000)
于 2013-07-24T04:51:42.363 回答