正在研究最新的 C# mongodb 驱动程序和 .NET 4.5.1。
我想在玩家之间进行一些定制的比赛。 假设我有以下模型。
public sealed class PlayerPoints
{
[BsonId]
public ObjectId PlayerId;
public DateTime CreateDate;
public int Points;
public int[] SeasonalPoints;
}
我希望能够获得特定 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]}
请注意,这里有 10 个季节。 我正在搜索一个查询,该查询返回根据排名排序的玩家列表。排名由提供的索引之间的点之和设置。
如果我查询第 9 季到第 10 季的排名,那么 someId3 是第一个,someId2 之后,someId1 是最后一个。 如果我查询第 7-9 季的排名,那么 someId1 是第一个,someId2 是第二个,someId3 是第三个。
我考虑过使用聚合,它会如何影响大约 100 万文档的性能,同时还会非常频繁地调用此查询。
澄清
主要问题是如何构建将产生上述结果的查询,次要问题是查询将从服务器消耗多少性能。
谢谢。
最佳答案
至少,如果托管服务器的机器与数据库的机器不同,您将获得改进的服务器性能。
另一方面,这可能意味着数据库机器可能不太“可用”,因为它忙于计算聚合结果。这是应该进行基准测试的东西,因为它因应用程序和时间而异。
这取决于用户负载、数据量、主机等。
至于查询,这是一个我验证过实际工作的程序:
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
}
},
};
}
}
}
关于C# MongoDB 基于点数组的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42923780/
导读:随着叮咚买菜业务的发展,不同的业务场景对数据分析提出了不同的需求,他们希望引入一款实时OLAP数据库,构建一个灵活的多维实时查询和分析的平台,统一数据的接入和查询方案,解决各业务线对数据高效实时查询和精细化运营的需求。经过调研选型,最终引入ApacheDoris作为最终的OLAP分析引擎,Doris作为核心的OLAP引擎支持复杂地分析操作、提供多维的数据视图,在叮咚买菜数十个业务场景中广泛应用。作者|叮咚买菜资深数据工程师韩青叮咚买菜创立于2017年5月,是一家专注美好食物的创业公司。叮咚买菜专注吃的事业,为满足更多人“想吃什么”而努力,通过美好食材的供应、美好滋味的开发以及美食品牌的孵
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
需求:要创建虚拟机,就需要给他提供一个虚拟的磁盘,我们就在/opt目录下创建一个10G大小的raw格式的虚拟磁盘CentOS-7-x86_64.raw命令格式:qemu-imgcreate-f磁盘格式磁盘名称磁盘大小qemu-imgcreate-f磁盘格式-o?1.创建磁盘qemu-imgcreate-fraw/opt/CentOS-7-x86_64.raw10G执行效果#ls/opt/CentOS-7-x86_64.raw2.安装虚拟机使用virt-install命令,基于我们提供的系统镜像和虚拟磁盘来创建一个虚拟机,另外在创建虚拟机之前,提前打开vnc客户端,在创建虚拟机的时候,通过vnc
这是针对我无法破坏的现有公共(public)API,但我确实希望对其进行扩展。目前,该方法采用字符串或符号或任何其他在作为第一个参数传递给send时有意义的内容我想添加发送字符串、符号等列表的功能。我可以只使用is_a吗?数组,但还有其他发送列表的方法,这不是很像ruby。我将调用列表中的map,所以第一个倾向是使用respond_to?:map。但是字符串也会响应:map,所以这行不通。 最佳答案 如何将它们全部视为数组?String的行为与仅包含String的Array相同:deffoo(obj,arg)[*arg].eac
我有一个像这样的ruby散列{"stuff_attributes"=>{"1"=>{"foo"=>"bar","baz"=>"quux"},"2"=>{"foo"=>"bar","baz"=>"quux"}}}我想把它变成一个看起来像这样的散列{"stuff_attributes"=>[{"foo"=>"bar","baz"=>"quux"},{"foo"=>"bar","baz"=>"quux"}]}我还需要保留键的数字顺序,并且键的数量是可变的。上面是super简化的,但我在底部包含了一个真实的例子。执行此操作的最佳方法是什么?附言还需要递归就递归而言,这是我们可以假设的:1)
我正在寻找用于Rails的优质管理插件。似乎大多数现有的插件/gem(例如“restful_authentication”、“acts_as_authenticated”)都围绕着self注册等展开。但是,我正在寻找一种功能齐全的基于管理/管理角色的解决方案——但不是简单地附加到另一个非基于角色的解决方案。如果我找不到,我想我会自己动手......只是不想重新发明轮子。 最佳答案 RyanBates最近做了两个关于授权的railscast(注意身份验证和授权之间的区别;身份验证检查用户是否如她所说的那样,授权检查用户是否有权访问资源
我正在根据Rakefile中的现有测试文件动态生成测试任务。假设您有各种以模式命名的单元测试文件test_.rb.所以我正在做的是创建一个以“测试”命名空间内的文件名命名的任务。使用下面的代码,我可以用raketest:调用所有测试require'rake/testtask'task:default=>'test:all'namespace:testdodesc"Runalltests"Rake::TestTask.new(:all)do|t|t.test_files=FileList['test_*.rb']endFileList['test_*.rb'].eachdo|task|n
我想要像“嘿那里”这样的东西变成,例如,#316583。我希望将任意长度的字符串“归结”为十六进制颜色。我不知道从哪里开始。我在想,每个字符串的MD5散列都是不同的-但如何将该散列转换为十六进制颜色数字? 最佳答案 你可以只取几位前几位:require'digest/md5'color=Digest::MD5.hexdigest('Mytext')[0..5] 关于ruby-如何使用Ruby基于字母数字字符串生成颜色?,我们在StackOverflow上找到一个类似的问题:
文章目录1.自动驾驶实战:基于Paddle3D的点云障碍物检测1.1环境信息1.2准备点云数据1.3安装Paddle3D1.4模型训练1.5模型评估1.6模型导出1.7模型部署效果附录show_lidar_pred_on_image.py1.自动驾驶实战:基于Paddle3D的点云障碍物检测项目地址——自动驾驶实战:基于Paddle3D的点云障碍物检测课程地址——自动驾驶感知系统揭秘1.1环境信息硬件信息CPU:2核AI加速卡:v100总显存:16GB总内存:16GB总硬盘:100GB环境配置Python:3.7.4框架信息框架版本:PaddlePaddle2.4.0(项目默认框架版本为2.3
我正在尝试整个BDD方法并想测试AMQP基于Vanilla的方面Ruby我正在写的应用程序。选择Minitest后作为与其他名副其实的蔬菜框架不同的平衡功能和表现力的测试框架,我着手编写此规范:#File./test/specs/services/my_service_spec.rb#Requirementsfortestrunningandconfigurationrequire"minitest/autorun"require"./test/specs/spec_helper"#Externalrequires#MinitestSpecsforEventMachinerequire