开发者

How to execute mongodb query from (C#).net core 2.1 class library?

I am using .net core 2.1 class library, and MongoDB.Driver.dll Version 1.7.0.4714 I have prepared a query in mongodb shell, and it is working fine. But I dont know how to execute the same query in c# code and fetch output in list. Can someone provide me sample code for the same?

Query is for getting hourly average data between specific date Firstly i have added $match for filtering specific data, all these data are coming from request. Then i have used $project for avoiding unnecessary columns, and at last i have performed $group for calculating average

Query that is working fine in mongodb shell (I used this software: NoSQLBooster for MongoDB):

db.DataMonitor.aggregate(
    [
        {
            $match:
            {
                RTC: { $gte:ISODate("2022-07-21T00:00:00.000+00:00"), $lt:ISODate("2022-07-22T00:00:00.000+00:00") },
                EnergymeterId: "AKSH1000000001000001"
            }
        },
        {
            $project:
            {
                monthDayYear: { $dateToString: { format: "%m-%d-%Y", date: "$RTC" } },
                A1: "$A1",
                RTC: { $dateToString: { format: "%m-%d-%Y", date: "$RTC" } }
            }
        },
        {
            $group:
            {
                _id:"$RTC", avg_val:{$avg:{ $convert: { input: "$A1", to: "double" } }}
            }
        }
    ]
)

I want to write this query in c# code and bind the result in following class: here RTC is date and A1 is average data

[BsonIgnoreExtraElements]
[DataCo开发者_运维技巧ntract]
public class GetAverageDataResBO
{
    [DataMember]
    public string RTC { get; set; }

    [DataMember]
    public double A1 { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜