0

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());
            }
        }

カウント値 (リツイート数) の降順で結果を並べ替える方法を知っている人はいますか?

4

1 に答える 1

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();
于 2013-01-27T13:09:31.527 に答える