1

我需要创建一个计数器来跟踪我网页中显示的多媒体项目上的“喜欢”/“心”,很可能是图像或视频,此功能非常标准,与 facebook / Instagram / Dribble 的情况非常相似.

我想要一些关于如何实现这种计数器的建议,我继续这个实现时要牢记它的最佳性能。

到目前为止,我的实体模型如下:

public class Portfolio:Entity
{
    public int UserId { get; set; }
    public string Url { get; set; }
    public string Thumbnail { get; set; }
    public string FullSizeImage { get; set; }
    public string Description { get; set; }
    public MediaType MediaType { get; set; }
}

请查看运球截图以供参考 在此处输入图像描述

我正在使用 C# 和 MongoDB 驱动程序/MongoRepository

4

1 回答 1

2

MongoDB supports atomic increment operations. You can see the docs for this here: http://docs.mongodb.org/manual/reference/operator/inc/

db.portfolio.update({ _id: 10},{ $inc: { likes: 1 } });

This will update the likes field by 1 of the portfolio with an _id of 10. Even if 2, or 10, different users do this at the same time, the increment operation will count them all.

In C#, there are a number of ways to do this, the easiest being to use the builders.

var query = Query<Portfolio>.EQ(x => x.UserId, 10);
var update = Update<Portfolio>.Inc(x => x.Likes, 1);

collection.Update(query, update);
于 2013-09-06T16:52:37.357 回答