MongoDB聚合管道C# [英] MongoDB Aggregation Pipeline C#

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

问题描述

我有以下Mongo查询:

I have the following Mongo query:

db.BusRatings.aggregate(
[     
   { $match: { VId: 2020}},
   { $project: {  vid: '$VId', sb:'$SvcRating.StaffBehavior'  ,bq: '$SvcRating.BusQuality' ,  src: '$SvcDetails.SrcNm', dst: '$SvcDetails.DestNm', py: '$SvcRating.Punctuality'}},    
   { $group: { _id: {vid: '$vid', src: '$src', dst: '$dst'}, numratings: {$sum: 1}, avgpy : { $avg : '$py'}, avgbq: { $avg: '$bq'},avgsb: { $avg: '$sb'}} },
   { $match : { numratings: { $gt: 3 } } },
   { $project: {_id: 0,  'vid' : '$_id.vid',  'src' : '$_id.src',  'dst' : '$_id.dst', 'avg': {'$divide':[ {'$add': [ '$avgpy', '$avgbq', '$avgsb' ]},3.0]}}},
   { $sort : { avg : -1 } }
 ]
)

在上面的管道中执行最后一个投影的C#代码是什么.我正在遵循此处文档中列出的示例:

What would be the C# code to do the last projection in the pipeline above. I am following the example listed at the documentation here:

http://mikaelkoskinen.net/mongodb-aggregation-framework-examples -in-c/

对于第一次比赛,我有以下代码,项目,组:

I have the following code for the first match,project,group :

 var match1 = new BsonDocument 
                { 
                    { 
                        "$match", 
                        new BsonDocument 
                            { 
                                {"VId", opId} 
                            } 
                    } 
                };


            var project1 = new BsonDocument 
                { 
                    { 
                        "$project", 
                        new BsonDocument 
                            { 
                                {  "vid", opId},
                                {  "sb", "$SvcRating.StaffBehavior"},
                                {   "bq", "$SvcRating.BusQuality"},
                                {   "src",   "$SvcDetails.SrcNm"},
                                {   "dst", "$SvcDetails.DestNm"} ,
                                {   "py",  "$SvcRating.Punctuality"}
                            } 
                    } 
                };

            var group = new BsonDocument 
                { 
                    { 
                        "$group", 
                        new BsonDocument 
                            { 
                                { "_id", new BsonDocument 
                                             { 
                                                {"vid", "$vid"},
                                                { "src", "$src"},
                                                { "dst", "$dst"}
                                             } 
                                }, 
                                { 
                                    "numratings", new BsonDocument 
                                                 { 
                                                     { "$sum", 1 } 
                                                 } 
                                }, 
                                {
                                    "avgpy", new BsonDocument
                                              {
                                                   { "$avg" , "$py"}
                                              }
                                }
                            }
                            }
                };

            var match2 = new BsonDocument 
                { 
                    { 
                        "$match", 
                        new BsonDocument 
                            { 
                                {"numratings", new BsonDocument
                                    {
                                        {"$gt",3}
                                    }    
                                } 
                            } 
                    } 
                };

推荐答案

尝试一下:

var project2 = new BsonDocument
{
    {
        "$project", new BsonDocument
        {
            {"_id", 0},
            { "vid", "$_id.vid" },
            { "src", "$_id.src" },
            { "dst", "$_id.dst" },
            { "avg", new BsonDocument
                {
                    { "$divide", new BsonArray 
                        {
                            new BsonDocument
                            {
                                {"$add", new BsonArray {"$avgpy", "$avgbq", "$avgsb" }}
                            }, 
                            3.0
                        }
                    }
                }
            }
        }
    }
};

这篇关于MongoDB聚合管道C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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