Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question can be solved by altering your map reduce, unless I am mistaken.</p> <p>My assumption from the comments is that what you are looking for is a sum of each hour, for each previous hour. This means that you can mapreduce on the <code>CollectionDateHour</code> rather than the <code>CollectionDate</code>. You can then control in the application where you want the <code>CollectionDateHour</code> to fall (for example you can decide if <code>6:45</code> is in the <code>7:00</code> group or in the <code>6:00</code> group.</p> <p>There is two points to change in your application, first handle the collection date hour:</p> <pre><code>for (int l = 1; l &lt; records.Length; l++) { ... var collectionDate = Convert.ToDateTime(BsonString.Create(abc[5])); var collectionDateHour = collectionDate.Minute&gt;0?collectionDate.AddHours(1).Hour:collectionDate.Hour; book1.Add("CollectionDateHour", BsonInt32.Create(collectionDateHour)); ... </code></pre> <p>Which is simply making the <code>CollectionDateHour</code> one more than the hour from <code>CollectionDate</code> if there is any minute part of <code>CollectionDate</code>. You can tune this to fit your logic.</p> <p>The second change is to adjust your map reduce function so that you are reducing based on the <code>CollectionDateHour</code> rather than the full string you have used previously. One way to do this would be to change your map function:</p> <pre><code>String map = @"function() { var newCollectionDate = this.CollectionDate.split(' ')[0] + (this.CollectionDateHour&lt;10?' 0':' ')+this.CollectionDateHour + ':00' emit(newCollectionDate, {CollectionDate : newCollectionDate, count: 1}); }"; </code></pre> <p>Which will set the collection date to the hour figure worked out in your application logic and and reduce on that. Given the above two changes, the following input:</p> <pre><code>{ "CollectionDate" : "08-08-2012 05:00" } { "CollectionDate" : "08-08-2012 05:15" } { "CollectionDate" : "08-08-2012 05:10" } { "CollectionDate" : "08-08-2012 04:00" } { "CollectionDate" : "08-08-2012 12:45" } { "CollectionDate" : "08-08-2012 12:46" } { "CollectionDate" : "08-08-2012 23:45" } </code></pre> <p>Will give these results:</p> <pre><code>{ "_id" : "08-08-2012 00:00", "value" : { "CollectionDate" : "08-08-2012 00:00", "count" : 1 } } { "_id" : "08-08-2012 04:00", "value" : { "CollectionDate" : "08-08-2012 04:00", "count" : 1 } } { "_id" : "08-08-2012 05:00", "value" : { "CollectionDate" : "08-08-2012 05:00", "count" : 1 } } { "_id" : "08-08-2012 06:00", "value" : { "CollectionDate" : "08-08-2012 06:00", "count" : 2 } } { "_id" : "08-08-2012 13:00", "value" : { "CollectionDate" : "08-08-2012 13:00", "count" : 2 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload