MongoDB聚合groupby id始终为null

时间:2016-03-19 03:56:16

标签: c# mongodb mongodb-query aggregation-framework

我正在尝试按天汇总我的小时系列,但我的代码出了点问题。

我的课程是:

function show_pass() {
    ...

}

function validateLogin(k) {
   ...
}
DN.DOM.dom_ready(function () {
var event_ids = [
    ["#login-form-dialog", "validateLogin", "submit", 1]
    ["#loginnotes", "validateLogin", "click", 1],
    ["#loginnotes2", "validateLogin", "click", 2],
    ["#show-pass", "show_pass", "click", ""],
]

for (var i = 0; i < event_ids.length - 1; i++) {

    var fN = window[event_ids[i][1]];


    if (typeof fN === 'function') {

        $(event_ids[i][0]).on(event_ids[i][2], function () {
            fN(event_ids[i][3]);
        })

    } else {
        console.log("Could not find function: " + i + "->" + event_ids[i][1]);
    }
}
});

我的聚合方法是:

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; }
}

使用此代码时,看起来像&#34; Value&#34;和&#34;伯爵&#34;没关系,但是&#34; _id&#34;永远是空的。我不知道为什么,但&#34;项目&#34;从那以后起作用&#34;价值&#34;价值就像我预期的那样。

我也试过这段代码:

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);

但是&#34; _id&#34;的返回结果就像我写的那样:

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}}}
            }
        }
    }
};

所以在两个代码中看起来都像#34;价值&#34;和&#34;伯爵&#34;没问题,但是&#34; _id&#34;是错的。 有什么提示吗?

1 个答案:

答案 0 :(得分: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}}}
                }
            }
        }