然后放松MongoDB中的C#集团聚集 [英] Unwind then Group aggregation in MongoDB C#

查看:127
本文介绍了然后放松MongoDB中的C#集团聚集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,新的C#2.0的MongoDB驱动程序和聚合管道。

I'm having some trouble with the new C# 2.0 MongoDB driver and the aggregation pipeline.

基本上,我试图返回内最流行的元素在对象上数组字段。字段类型为:的IList<串GT; FavouritePlaceIds {搞定;组; }

Basically, I'm trying to return the most popular elements within an array field on the object. The field type is: IList<string> FavouritePlaceIds { get; set; }.

我有正按预期以下MongoDB的聚合:

I have the following MongoDB aggregation which is working as expected:

db.users.aggregate([
    { $unwind : "$FavouritePlaceIds" },
    { $group: { "_id": "$FavouritePlaceIds", "count": {$sum: 1}}}, 
    { $sort : { "count": -1 }}
])

不过,这个问题目前正试图翻译成使用新的驱动程序的MongoDB 2.0的C#代码。我一直在使用下面的链接与聚合管道帮助:的 http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/crud/reading/#unwind

However, the issue is now trying to translate that into C# code using the new MongoDB driver 2.0. I've been using the following link for help with the aggregation pipeline: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/crud/reading/#unwind

我已经走到这一步,我的汇聚管道以下内容:

I've got the following so far for my aggregation pipeline:

var pipeline = usersCollection.Aggregate()
                .Unwind(i => i.FavouritePlaceIds)
                .Group(i => i.FavouritePlaceIds, g => new { FavouritePlaceIds = g.Key, Count = g.Count() })
                .SortByDescending(i => i.Count);

当我去编译代码时,我得到以下信息:

When I go to compile that code, I get following message:

BsonDocument'不包含'FavouritePlaceIds的定义,并没有扩展方法'FavouritePlaceIds接受型BsonDocument'的第一个参数可以发现...

'BsonDocument' does not contain a definition for 'FavouritePlaceIds' and no extension method 'FavouritePlaceIds' accepting a first argument of type 'BsonDocument' could be found...

该错误是在第一个参数存在的( I => i.FavouritePlaceIds )集团()方法。

The error is occuring on the first parameter (i => i.FavouritePlaceIds) of the Group() method.

读数下组部分提供的链接笔记,它提到:

Reading the notes at the link provided under the group section, it mentions that:

由于$平仓是一种投射,你必须提供一个返回类型。

Because $unwind is a type of projection, you must provide a return type.

所以,我假设我M没有指定正确的返回类型,这就是为什么它的期待BsonDocument对象,并没有进行编译。

So, I'm assuming that I'm not specifying a correct return type, which is why it's expecting a BsonDocument object, and failing to compile.

所以,我怎么可以指定正确的返回类型的组的方法来使用?

So, how can I specify the correct return type to use in the Group method?

推荐答案

当你让开卷推断它将使用集合类型为 TResult 的类型参数和 BsonDocument TNewResult

When you let Unwind infer the type parameters it will use the collection type for TResult and BsonDocument for TNewResult.

如果你想使用特定类型的,而不是 BsonDocument 您需要添加这种类型的参数:

If you want to use a specific type and not BsonDocument you need to add these type parameters:

var pipeline = usersCollection.Aggregate()
    .Unwind<OriginalType, NewResultType>(....

与往常一样,你需要确保该操作不会返回的东西,可以是此类型。

As always, you need to make sure the operation actually returns something that can be of that type.

这篇关于然后放松MongoDB中的C#集团聚集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆