2017-02-25 2 views
1

gehören kann ich nächste Dummy-Dataset haben:MongoDB Aggregation - Gruppe von Datumsbereichen, wo ein Dokument mehrere Bereiche

{"id": 1, "date": "2017-04-01", "total": 1} 
{"id": 2, "date": "2017-04-03", "total": 2} 
{"id": 3, "date": "2017-04-10", "total": 1} 
{"id": 4, "date": "2017-04-11", "total": 1} 

auch habe ich etwas fester Wert count_of_days. Lassen Sie es gleich 5 (count_of_days = 5)

ich als nächste Gruppen erstellen muß:

{"2017-04-01 + 5 days": count: 2, sum_of_total: 3} 
{"2017-04-03 + 5 days": count: 1, sum_of_total: 2} 
{"2017-04-10 + 5 days": count: 2, sum_of_total: 2} 
{"2017-04-11 + 5 days": count: 1, sum_of_total: 1} 

wo count ist Anzahl der Dokumente, das Datumsfeld in Bereich ist [date; date + count_of_days]

so für Dokument mit id=1 wir solche Gruppe haben

{"2017-04-01 + 5 days": count: 2, sum_of_total: 3} 

weil

doc 1 date ist in Reichweite [2017-04-01; 2017-04-06 (+ 5 days)] und

doc 2 date2017-04-03 ist in Reichweite [2017-04-01; 2017-04-06 (+ 5 days)]

Wie kann ich tun dies mit aggregation framework oder vielleicht Map/Reduce?

das Hauptziel ist, die Produktivität und Rechengeschwindigkeit

+0

sorry, aber ist nicht klar, was Sie zu fragen, versuchen, versuchen, Ihre Frage Refactoring. – leonziyo

+0

@leonziyo Ich habe versucht, getan –

+0

ist Ihr Datum ein String Datentyp oder Datum Datentyp? – leonziyo

Antwort

0

Ich bin nicht sicher, wie das exakte Ergebnis zu erreichen, die für Sie gefragt, aber ich war zu dem zu einer Gruppe von Dokumenten basierend auf einem festen Zeitintervall anstelle ein Intervall relativ Lage Aufzeichnung.

ich Ihre Dokumente gehe davon wie folgt aussehen:

{ 
    "_id" : ObjectId("58b36eb4b4af453e43480473"), 
    "id" : 1.0, 
    "date" : ISODate("2017-04-02T00:21:20.201Z"), 
    "total" : 1.0 
} 

Aggregation Abfrage:

// change this as you need, currently it is the number of milliseconds in 5 days 
var interval = 1000 * 60 * 60 * 24 * 5; 

db.collection.aggregate([ 
    { 
     $project: { 
      timestamp: { 
       $divide : [ 
        { 
         $subtract: [ 
          { 
           $subtract: [ 
            '$date', 
            new Date("1970-01-01") 
           ] 
          }, 
          { 
           $mod: [ 
            { 
             $subtract: [ 
              '$date', 
              new Date("1970-01-01") 
             ] 
            }, 
            interval 
           ] 
          } 
         ] 
        }, 
        interval 
       ] 
      }, 

      date: 1, 

      total: 1, 

      id: 1 
     }  
    }, 

    { 
     $group: { 
      _id: { 
       timestamp: '$timestamp' 
      }, 
      sum_of_total: { $sum: '$total' }, 
      count: { $sum: 1 }    
     } 
    }, 

    { 
     $project: { 
      _id: 0, 
      count: 1, 
      sum_of_total: 1, 
      rangeStart: { $add: [new Date(0), { $multiply: ['$_id.timestamp', interval] } ] } 
     } 
    }, 

    { 
     $project: { 
      _id: 0, 
      count: 1, 
      sum_of_total: 1, 
      rangeStart: 1, 
      rangeEnd: { $add: [ "$rangeStart", interval ] } 
     } 
    } 
]) 
Verwandte Themen