我在C#中编写了一个方法,它从mongoDB中检索推文,并希望根据转推的数量对作者进行计数和排序。
现在,该方法已经执行map和reduce,并按以下方式返回未排序的结果:
public void RetweetsCount()
{
string wordMap = @"function wordMap() {
var usernameOrigin = this.text.match(/\brt\s*@(\w+)/i);
if (usernameOrigin === null) {
return;
}
// loop every word in the document
emit(usernameOrigin[1], { count : 1 });
}";
string wordReduce = @"function wordReduce(key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i].count;
}
return { count : total };
}";
var options = new MapReduceOptionsBuilder();
options.SetOutput(MapReduceOutput.Inline);
var results = collection.MapReduce(wordMap, wordReduce, options);
foreach (var result in results.GetResults())
{
Console.WriteLine(result.ToJson());
}
}
是否有人知道降序计数值(转发数量)的排序结果如何?
答案 0 :(得分:2)
这是解决方案。从MapReduce中检索结果后,我首先将IEnumerable转换为列表,然后按照以下方式对列表进行排序:
var results = collection.MapReduce(wordMap, wordReduce, options);
IEnumerable<BsonDocument> resultList = results.GetResults();
List<BsonDocument> orderedList = resultList.ToList().OrderByDescending(x => x[1]).ToList();