1

.NET 用の C# ドライバーで MongoDB を使用して次のことを行うにはどうすればよいですか。

次のコレクションがあるとします (構文エラーがある場合は無視してください)。

{_id: "2312321321sdd", "Ticker": "AAPL", "CompanyName" : "APPLE", "Viewed": "2013-05-13"}
{_id: "2312321321sde", "Ticker": "AAPL", "CompanyName" : "APPLE", "Viewed": "2013-05-12"}
{_id: "2312321321sdf", "Ticker": "GOOG", "CompanyName" : "GOOGLE", "Viewed": "2013-05-12"}
{_id: "2312321321sdg", "Ticker": "MSFT", "CompanyName" : "MICROSOFT", "Viewed": "2013-05-12"}
{_id: "2312321321sdh", "Ticker": "MSFT", "CompanyName" : "MICROSOFT", "Viewed": "2013-05-11"}

MongoDB ドライバーで C# および/または LINQ を使用して、最新の 3 つの表示アイテムだけでなく DISTINCT も取得する方法を教えてください。同じ TICKER の二重アップはありません。SO 理想的には、以下が返されます。

{_id: "2312321321sdd", "Ticker": "AAPL", "CompanyName" : "APPLE", "Viewed": "2013-05-13"}
{_id: "2312321321sdf", "Ticker": "GOOG", "CompanyName" : "GOOGLE", "Viewed": "2013-05-12"}
{_id: "2312321321sdg", "Ticker": "MSFT", "CompanyName" : "MICROSOFT", "Viewed": "2013-05-12"}

前もって感謝します....

4

1 に答える 1

1

I can't say this either uses LINQ or even much C#, but it's what you're after in MongoDB C# as it is today. Unfortunately, Collection.AsQueryable() won't get you far.

Aggregation Framework or MapReduce are the way to go. The set of distinct tickers needs to be modest, but here's AF:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MongoCollection<Tick> coll =
                new MongoClient("mongodb://11.11.11.11/test").GetServer()
                                                                .GetDatabase("test")
                                                                .GetCollection<Tick>("tix");

            var group = new BsonDocument
                {
                    {
                        "$group",
                        new BsonDocument
                            {
                                {
                                    "_id",  "$CompanyName"
                                },
                                {
                                    "LastViewed", new BsonDocument
                                        {
                                            {
                                                "$max", "$Viewed"
                                            }
                                        }
                                }
                            }
                    }
                };

            foreach (
                var t in
                    coll.Aggregate(group,
                                   new BsonDocument("$sort", new BsonDocument("LastViewed", -1)),
                                   new BsonDocument("$limit", 3))
                        .ResultDocuments)
            {
                Console.WriteLine("{0} last viewed {1}", t["_id"], t["LastViewed"]);
            }
    }

    public class Tick
    {
        public string Id { get; set; }
        public string Ticker { get; set; }
        public string CompanyName { get; set; }
        public string Viewed { get; set; }
    }
}
于 2013-05-13T02:49:53.760 に答える