2014-01-17 18 views
5

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 } 
+0

überprüfen Sie durch dieses Dokument mongodb der Gruppierung gehen kann: http://docs.mongodb.org/manual/reference/method/db.collection.group/ –

+0

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

Antwort

14

Sie können sie tun mit Aggregationsframework.

Gib mir die Anzahl der Personen pro Monat/Jahr

db.collection.aggregate(
    {$project : { 
     year : {$year : "$startTime"}, 
     month : {$month : "$startTime"} 
    }}, 
    {$group : { 
     _id : {year : "$year", month : "$month"}, 
     count : {$sum : 1} 
    }} 
) 

Gib mir die Anzahl der Personen pro Monat mit einer Dauer> 5000ms

db.collection.aggregate(
    {$project : { 
     year : {$year : "$startTime"}, 
     month : {$month : "$startTime"}, 
     duration: {$cond: [{$gt: ['$duration', 5000]}, 1, 0]} 
    }}, 
    {$group : { 
     _id : {year : "$year",month : "$month"}, 
     duration : {$sum : "$duration"} 
    }} 
) 

Weitere Informationen erhalten Sie Aggregation Framework.

+0

Ich habe die erste ausprobiert, aber ich scheine die folgende Ausnahme zu erhalten: Befehl 'aggregate' ist fehlgeschlagen: exception: kann nicht vom BSON-Typ NumberLong64 nach Datum konvertieren (Antwort: {"errmsg": "Ausnahme: kann nicht konvertieren von BSON Typ NumberLong64 bis Datum "," Code ": 16006," ok ": 0.0}) – ChrisGeo

+0

habe ich vergessen zu erwähnen. Um die Operatoren $ year und $ month zu verwenden, sollte startTime in einem Datumsformat vorliegen. Es wird nicht mit Long funktionieren. –

+0

Kann ich diese Konvertierung on-the-fly machen? Ich habe einige Wege ausprobiert, aber keiner scheint zu funktionieren. – ChrisGeo

1

Bitte Das kompatible Datenformat von MongoDB finden Sie unter http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date

Und unten ist die Möglichkeit, Aggressionen zu testen.

rs1:PRIMARY> 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "1", "LastUpdatedOn" : new Date() , "company" : "microsoft" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "2", "LastUpdatedOn" : new Date() , "company" : "google" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "3", "LastUpdatedOn" : new Date() , "company" : "ibm" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "4", "LastUpdatedOn" : new Date() , "company" : "cisco" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "5", "LastUpdatedOn" : new Date() , "company" : "dbversity.com" }) 
rs1:PRIMARY> 
rs1:PRIMARY> db.dbversitycol.find() 
{ "_id" : "1", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.203Z"), "company" : "microsoft" } 
{ "_id" : "2", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.207Z"), "company" : "google" } 
{ "_id" : "3", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.210Z"), "company" : "ibm" } 
{ "_id" : "4", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.213Z"), "company" : "cisco" } 
{ "_id" : "5", "LastUpdatedOn" : ISODate("2014-11-28T13:09:14.035Z"), "company" : "dbversity.com" } 
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.dbversitycol.aggregate(
... { 
...  "$project" : 
...  { 
...  _id : 0, 
...  "datePartDay" : {"$concat" : [ 
...   {"$substr" : [{"$dayOfMonth" : "$LastUpdatedOn"}, 0, 2]}, "-", 
...   {"$substr" : [{"$month" : "$LastUpdatedOn"}, 0, 2]}, "-", 
...   {"$substr" : [{"$year" : "$LastUpdatedOn"}, 0, 4]} 
...  ] } 
...  } 
... }, 
... { "$group" : 
...  { "_id" : "$datePartDay", "Count" : { "$sum" : 1 } } 
...  } 
...) 
{ "result" : [ { "_id" : "28-11-2014", "Count" : 5 } ], "ok" : 1 } 
rs1:PRIMARY> 
rs1:PRIMARY> 



rs1:PRIMARY> db.dbversitycol.aggregate(
...  {$project : { 
...   year : {$year : "$LastUpdatedOn"}, 
...   month : {$month : "$LastUpdatedOn"} 
...  }}, 
...  {$group : { 
...   _id : {year : "$year", month : "$month"}, 
...   count : {$sum : 1} 
...  }} 
...) 
{ 
     "result" : [ 
       { 
         "_id" : { 
           "year" : 2014, 
           "month" : 11 
         }, 
         "count" : 5 
       } 
     ], 
     "ok" : 1 
} 
rs1:PRIMARY> 

können Sie mehr aufeinander bezogene Kommentare http://www.dbversity.com/

Verwandte Themen