基于点数组的 C# MongoDB 排名

Ori*_*ael 2 c# mongodb mongodb-.net-driver

使用最新的 C# mongodb 驱动程序和 .NET 4.5.1。

我想在玩家之间进行一些定制的比赛。假设我有以下模型。

public sealed class PlayerPoints
{
    [BsonId]
    public ObjectId PlayerId;

    public DateTime CreateDate;

    public int Points;
    public int[] SeasonalPoints;

}
Run Code Online (Sandbox Code Playgroud)

我希望能够在特定SeasonalPoints索引之间获得玩家的排名。

一个例子:

 {PlayerId : someId1, CreateDate : <someCreateDate>, Points : 1000, SeasonalPoints : [100,100,100,100,100,100,100,100,100,100,100]}
 {PlayerId : someId2, CreateDate : <someCreateDate>, Points : 1000, SeasonalPoints : [100,100,100,100,100,100,100,100,50,150,100]}
 {PlayerId : someId3, CreateDate : <someCreateDate>, Points : 1100, SeasonalPoints : [200,100,100,100,100,100,100,100,0,0,300]}
Run Code Online (Sandbox Code Playgroud)

请注意,这里有 10 个季节。我正在搜索一个查询,该查询根据玩家的排名返回排序的玩家列表。排名由提供的索引之间的点的总和设置。

如果我查询第 9 季到第 10 季的排名,那么 someId3 是第一个,someId2 之后,someId1 是最后一个。如果我在第 7-9 季查询排名,那么 someId1 是第一,someId2 是第二,someId3 是第三。

我考虑过使用聚合,它会如何影响大约 100 万个文档的性能,同时这个查询也会被非常频繁地调用。

澄清

主要问题是如何构建将产生上述结果的查询,次要问题是查询将从服务器消耗多少性能。

谢谢。

Eya*_*rry 5

至少,如果托管服务器的机器与数据库的机器不同,您将获得改进的服务器性能。

另一方面,这可能意味着数据库机器可能不太“可用”,因为它太忙于计算聚合结果。这是应该进行基准测试的东西,因为它因应用程序而异,并且不时变化。

这取决于用户负载、数据量、主机等。

至于查询,这是我验证实际工作的程序:

using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

namespace MongoAggregation
{

public sealed class PlayerPoints
{
    public ObjectId Id { get; set; }

    //Note that mongo addresses everything as UTC 0, so if you store local time zone values, make sure to use this attribute
    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime CreateDate { get; set; }

    public int Points { get; set; }
    //note that your model did not allow a player to not participate in some season, so I took the liberty of introducing a new sub document.
    //It is better to create sub documents that store metadata to make the query easier to implement
    public int[] SeasonalPoints { get; set; }
}

class Program
{

    static void Main(string[] args)
    {
        //used v 2.4.3 of C# driver and v 3.4.1 of the db engine for this example
        var client = new MongoClient();
        IMongoDatabase db = client.GetDatabase("agg_example");

        var collectionName = "points";
        db.DropCollection(collectionName);

        IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>(collectionName);
        IEnumerable<BsonDocument> data = GetDummyData().Select(d=>d.ToBsonDocument());

        collection.InsertMany(data);

        //some seasons to filter by - note transformation to zero based
        var seasons = new[] {6, 7};

        //This is the query body:
        var seasonIndex = seasons.Select(i => i - 1);

        //This shall remove all un-necessary seasons from aggregation pipeline
        var bsonFilter = new BsonDocument { new BsonElement("Season", new BsonDocument("$in", new BsonArray(seasonIndex))) };

        var groupBy = new BsonDocument// think of this as a grouping with an anonyous object declaration
        {
             new BsonElement("_id", "$_id"),//This denotes the key by which to group - in this case the player's id
             new BsonElement("playerSum", new BsonDocument("$sum", "$SeasonalPoints")),//We aggregate the player's points after unwinding the array
             new BsonElement("player", new BsonDocument("$first", "$$CURRENT")),// preserve player reference for projection stage
        };

        var sort = Builders<BsonDocument>.Sort.Descending(doc => doc["playerSum"]);

        var unwindOptions = new AggregateUnwindOptions<BsonDocument>
        {
            IncludeArrayIndex = new StringFieldDefinition<BsonDocument>("Season")
        };

        var projection = Builders<BsonDocument>.Projection.Expression((doc => doc["player"]));

        List<BsonValue> sorted = collection
            .Aggregate()
            .Unwind(x=>x["SeasonalPoints"], unwindOptions)
            .Match(bsonFilter)
            .Group(groupBy)
            .Sort(sort)
            .Project(projection)
            .ToList();

    }

    private static IEnumerable<PlayerPoints> GetDummyData()
    {
        return new[]
        {
            new PlayerPoints
            {
                CreateDate = DateTime.Today,
                SeasonalPoints = Enumerable.Repeat(100,7).ToArray()
            },
            new PlayerPoints
            {
                CreateDate = DateTime.Today,
                SeasonalPoints = new []
                {
                    100,100,100,100,100,150,100
                }
            },
            new PlayerPoints
            {
                CreateDate = DateTime.Today,
                SeasonalPoints = new []
                {
                    100,100,100,100,100,0,300
                }
            },
        };
    }
}
}
Run Code Online (Sandbox Code Playgroud)