Saat dizimi güne toplu olarak birleştirmeye çalışıyorum ancak kodumda bir sorun var.MongoDB aggregate groupby kimliği her zaman boş
Benim sınıftır:
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; }
}
Benim agrega yöntemdir:
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);
bu kodu kullanarak, "Değeri" ve benziyor iken
Tamam ama "_ID" her zaman null "Sayı" . Neden bilmiyorum, ama "değer" değerinden beri "proje" çalışıyor, beklediğim gibi.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}}}
}
}
}
};
ama yazdım olduğu gibi "_ID" için dönen sonucu:
{
"_id":"{ month: { $month: '$Hour' }, day: { $dayOfMonth: '$Hour' }, year: { $year: '$Hour' } }",
"Value":16210.0,
"Count":3.0
}
Yani hem kodları gibi "Değer" ve "Kont bakar
Ben de bu kod çalıştı "Tamam, ama" _id "yanlış. Herhangi bir ipucu?
Iyi aksi halde hata alırsınız '' $ grup adı' ile _id' gerekir. Ama sağladığınız şey (aslında her iki durumda da) bir "dizgidir" ve "BsonDocument" değil. Bunun yerine bir 'BsonDocument' kullanın. –
@BlakesSeven önerdiğiniz örneği yazabilir misiniz? –