首页 文章

MongoDB聚合组ID始终为null

提问于
浏览
0

我试图按天汇总我的小时系列,但我的代码有问题 .

我的 class 是:

public class Serie {  
    [BsonId]
    public ObjectId Id { get; set; }

    [BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
    public DateTime Hour { get; set; }

    public ObjectId UserId { get; set; }
    public ObjectId AppId { get; set; }

    public AppHourSerieMetric Type { get; set; }

    public double Value { get; set; }
}

我的聚合方法是:

var args = new AggregateArgs();

var pipeline = new[]
{
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.EQ(c => c.UserId, user).ToBsonDocument()),
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.EQ(c => c.Type, metric).ToBsonDocument()),
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.GTE(c => c.Hour, from).ToBsonDocument()),
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.LTE(c => c.Hour, to).ToBsonDocument()),
    new BsonDocument
    {
        {
            "$project",
            new BsonDocument
            {
                {"key", "$dateToString: { format: '%Y-%m-%d', date: '$Hour' }"},
                {"value", "$Value"}

            }
        }
    },

    new BsonDocument
    {
        {
            "$group", new BsonDocument
            {
                {"key", "$key"},
                {"Value", new BsonDocument {{"$sum", "$value"}}},
                {"Count", new BsonDocument {{"$sum", (double) 1}}}
            }
        }
    }
};

args.Pipeline = pipeline;
args.AllowDiskUse = true;

var aggregate = _repo.GetCollection().Aggregate(args);

使用此代码时,看起来像“值”和“计数”是可以的,但“_id”始终为空 . 我不知道为什么,但“项目”有效,因为“ Value ” Value 符合我的预期 .

我也试过这段代码:

var pipeline = new[]
{
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.EQ(c => c.UserId, user).ToBsonDocument()),
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.EQ(c => c.Type, metric).ToBsonDocument()),
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.GTE(c => c.Hour, from).ToBsonDocument()),
    new BsonDocument("$match",
        Query<AppHourSerieEntity>.LTE(c => c.Hour, to).ToBsonDocument()),                  
    new BsonDocument
    {
        {
            "$group", new BsonDocument
            {
                {"_id", "{ month: { $month: '$Hour' }, day: { $dayOfMonth: '$Hour' }, year: { $year: '$Hour' } }"},
                {"Value", new BsonDocument {{"$sum", "$Value"}}},
                {"Count", new BsonDocument {{"$sum", (double) 1}}}
            }
        }
    }
};

但是“_id”的返回结果是我写的:

{  
   "_id":"{ month: { $month: '$Hour' }, day: { $dayOfMonth: '$Hour' }, year: { $year: '$Hour' } }",
   "Value":16210.0,
   "Count":3.0
}

所以看起来在两个代码中“Value”和“Count”都可以,但“_id”是错误的 . 有小费吗?

1 回答

  • 1

    使用 BsonDocument 而不是 _id 中的字符串 $group

    new BsonDocument
            {
                {
                    "$group", new BsonDocument
                    {
                        {
                            "_id", 
                            new BsonDocument 
                              { 
                                { "month", new BsonDocument("$month", "$Hour") }, 
                                { "day", new BsonDocument("$dayOfMonth", "$Hour") }, 
                                { "year", new BsonDocument("$year", "$Hour") }
                              }
                        },
                        {"Value", new BsonDocument {{"$sum", "$Value"}}},
                        {"Count", new BsonDocument {{"$sum", (double) 1}}}
                    }
                }
            }
    

相关问题