Ich habe die folgende Sammlung in mongoDBMongoDB Aggregatabfrage Gruppe von Daten
{ _id, startTime, duration }
So ist die Grundidee ist, dass eine Kamera für Menschen schaut und wenn es eine Person erkennt er die Startzeit spart und sobald eine Person verschwindet es speichert die Dauer. Also sagt die Entität im Grunde: "Eine Person erschien zur X-Zeit und war für Y Millisekunden im Kamerabereich". Sowohl startTime als auch duration sind numerische Werte.
Also, ich möchte wie verschiedene Abfragen durchführen: 1. Geben Sie mir die Anzahl der Personen pro Monat/Jahr 2. Geben Sie mir die Anzahl der Personen pro Monat mit einer Dauer> 5000ms
usw.
Ich bin ziemlich neu in MongoDB und ich habe ein bisschen Probleme mit der Aggregation Framework, so würde ich mich freuen, wenn jemand mir eine Idee gibt, wie man eine Abfrage wie die oben genannten, um eine Art von Vorsprung.
EDIT:
Ok ich ein paar Versuche gemacht haben, aber kein Glück. Gerade jetzt meine Objekte haben diese Form:
{
"_id" : ObjectId("52de407c75895eaf5ea99715"),
"startTime" : "new Date('02 01 2011 08:36:54')",
"duration" : 27000
}
und ich versuche, diese Abfrage:
db.collection.aggregate(
{$project : {
year : {$year : "$startTime"}
}},
{$group : {
_id : {year : "$year"},
count : {$sum : 1}
}}
)
aber ich erhalte die folgende Ausnahme:
Error occurred in performing aggregation
Command 'aggregate' failed: exception: can't convert from BSON type String to Date (response: { "errmsg" : "exception: can't convert from BSON type String to Date", "code" : 16006, "ok" : 0.0 })
Type: MongoDB.Driver.MongoCommandException
Stack: at MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection connection)
at MongoDB.Driver.MongoCollection.RunCommandAs[TCommandResult](IMongoCommand command, IBsonSerializer resultSerializer, IBsonSerializationOptions resultSerializationOptions)
at MongoDB.Driver.MongoCollection.RunCommandAs[TCommandResult](IMongoCommand command)
at MongoDB.Driver.MongoCollection.Aggregate(IEnumerable`1 operations)
at MangoUI.ComAggregate.kRemove_Click(Object sender, EventArgs e)
Inputs::
Command: aggregate
Ok: False
ErrorMsg: exception: can't convert from BSON type String to Date
Request: { "aggregate" : "person", "pipeline" : [{ "$project" : { "year" : { "$year" : "$startTime" } } }, { "$group" : { "_id" : { "year" : "$year" }, "count" : { "$sum" : 1 } } }] }
Response: { "errmsg" : "exception: can't convert from BSON type String to Date", "code" : 16006, "ok" : 0.0 }
überprüfen Sie durch dieses Dokument mongodb der Gruppierung gehen kann: http://docs.mongodb.org/manual/reference/method/db.collection.group/ –
Wenn Sie diese Art von Abfragen durchführen möchten, sollten Sie die 'startTime' Ihrer Dokumente so ändern, dass sie ein' Date' statt einer Zahl ist. – JohnnyHK