Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I compose a date range query in Mongodb's aggregation framework?
    primarykey
    data
    text
    <p>My goal is to perform a match and group based on date range using mongodb 2.2 and latest c# driver.</p> <p>I'm having trouble creating a match query in the aggregation framework that uses datetimes. I dont know if my datatype is incorrect (the poco has a datetime and the object was utc at save) or if the query is incorrect, which is more likely.</p> <pre><code>//Here is the doc format that I'm querying: { "DateTime" : new Date("2/20/2013 17:29:03"), "DateTimeString" : "20130220", "PurchaseAmount" : 1.91, "ProductId" : "51293ac844da932e941fa2c4", "_id" : "20130220/51293ac844da932e941fa2c4" } </code></pre> <p>//and here is the aggregation query i'm attempting</p> <pre><code> var timeStamp = DateTime.UtcNow.AddDays(-20); var startDate = timeStamp.Date; var endDate = timeStamp.AddDays(1).Date; var match = new BsonDocument { { "$match", new BsonDocument { { "ProductId" , "51293ac844da932e941fa2c4"} } } }; var match2 = new BsonDocument { { "$match", new BsonDocument { *** { "$gte" , new BsonDocument{{"DateTime", timeStamp}} } //, *** // { "$lt" , endDate} } } }; var group = new BsonDocument { { "$group", new BsonDocument { { "_id", new BsonDocument { { "ProductId" , "$ProductId"} ,{ "DateTime" , "$DateTime" } } }, { "AvgOrder", new BsonDocument { { "$avg", "$PurchaseAmount" } } } } } }; var pipeline = new[] { match, match2, group }; var aggregateResult = dbSession.GetCollection&lt;RecentOrders&gt;().Aggregate(pipeline); </code></pre> <p>Please see the <em>*</em>. I want to provide specific time ranges and calculate the average orders for that customer over that interval (by minute, hour, or day ranges). I believe that I have it working except for the date range query portion (match2). I think the issue is the tricky combination of new BsonDocuments and the query. Thanks so much if you can help!</p>
    singulars
    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.
 

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