1

如何使用 MongoDB 和适用于 .NET 的 C# 驱动程序执行以下操作。

假设我有以下集合(如果有语法错误则忽略):

{_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"}

将 C# 和/或 LINQ 与 MongoDB 驱动程序一起使用,我如何获得最新的 3 个查看项目以及 DISTINCT,因此没有相同 TICKER 的双倍。所以理想情况下会返回以下内容:

{_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

我不能说这要么使用 LINQ 甚至很多 C#,但它就是你在 MongoDB C# 中所追求的,就像今天一样。不幸的是,Collection.AsQueryable()不会让你走远。

聚合框架或 MapReduce 是要走的路。不同的代码集需要适度,但这里是 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 回答