MongoDb Driver 2.0 C#Filter和Aggregate

时间:2015-04-22 16:58:20

标签: c# mongodb filtering grouping facets

我正在玩mongodb 2.0的新驱动程序,并寻找添加一些分面搜索(临时移动,在使用弹性搜索之前)。

以下是我为构建agreggation而创建的方法。我猜它应该有用。

因为参数I在方法中也传递了filterdefinition。 但我不知道如何限制我对过滤器的聚集。

任何想法???

    private void UpdateFacets(SearchResponse response, FilterDefinition<MediaItem> filter, ObjectId dataTableId)
    {

        response.FacetGroups =new List<SearchFacetGroup>();

        SearchFacetGroup group = new SearchFacetGroup()
        {
            Code = "CAMERAMODEL",
            Display = "Camera model",
            IsOptional = false
        };

        using (IDataAccessor da = NodeManager.Instance.GetDataAccessor(dataTableId))
        {
            var collection = da.GetCollection<MediaItem>();
            var list = collection.Aggregate()
                .Group(x => ((ImageMetaData) x.MetaData).Exif.CameraModel, g => new { Model = g.Key, Count = g.Count() })
                .ToListAsync().Result;


            foreach (var l in list)
            {
               group.Facets.Add(new SearchFacetContainer()
               {
                   Code = l.Model,
                   Display = l.Model,
                   Hits = l.Count, 
                   IsSelected = false
               });
            }
        }

        response.FacetGroups.Add(group);
    }

1 个答案:

答案 0 :(得分:3)

我还没有使用过facet,但是对于Mongo驱动程序,Aggregate的.Match操作接受了filterdefinition。

collection1.Aggregate().Match(filter)