Aggregate $match with ISODate
See original GitHub issueHey guys -
Have been scratching my head today on this one.
TL;DR
When using a .aggregate([{ $match: }])
, I’m unable to get results back when matching dates. I am able to get results back from the command line via aggregation matching, as well as Robomongo, and from Model.find() when using a date.
Tests - Newbie Disclaimer
I went ahead and forked so that I could add in some tests, although it appears that aggregate.test.js does not run any of its tests. At least I’m getting a 0 tests complete when doing T="-g 'aggregate.test.js' -R spec" make test
- console.logs() work inside the describe() levels, but not instead the it() levels. I’m new to tests a bit so I’ll keep working on that.
Some details
Looking to $match
a set of data to a date range, so that I can pipeline it to a $group
.
This works and returns data from Robomongo (as well as the mongo shell):
db.points.aggregate([
{
'$match' : { 'createdAt' : { '$gte' : ISODate('2013-07-26T18:23:37.000Z') } }
},
{
'$group' : { '_id' : '$rfid' }
}
]);
This works and returns data from Model.find()
Points.find({
createdAt: {
$gt: '2013-07-26T18:23:37.000Z',
}
}, callback);
Additionally, this aggregate pipeline worked before, where vendorId was a string for the $match values
Points.aggregate([
{ $match : { vendorId : vendorId } },
{ $group : { _id : '$rfid', amount: { $max : '$amount' } } },
{ $project: { _id: 0, amount: 1, rfid: '$_id' } },
{ $sort : { points: -1 } },
{ $limit : 25 }
], callback);
The ‘bug’
I am not able to retrieve any results from Points.aggregate([])
when using a $match
that contains any variation of this:
$match : { createdAt : { $gt: utcBounds.start, $lt: utcBounds.end } } // moment utc dates that work in .find() as well
$match : { createdAt : Date('2013-07-26T18:23:37.000Z') }
$match : { createdAt : Date('2013-07-26T18:23:37.000Z').toISOString() }
$match : { createdAt : Schema.Types.Date('2013-07-26T18:23:37.000Z') }
$match : { createdAt : '2013-07-26T18:23:37.000Z' }
$match : { createdAt : { $gt: '2013-07-26T18:23:37.000Z' } }
Sample Data
If it helps, pulling up a record from MongoLabs looks like this:
{
"rfid": "0004980F0A9E2A80",
"type": "debit",
"vendorId": "51e837ce2d494abc32000001",
"fullName": "Stephen Rivas Jr.",
"_id": {
"$oid": "51f2bea9560b54b437000001"
},
"createdAt": {
"$date": "2013-07-26T18:23:37.000Z"
},
"amount": 15,
"__v": 0
}
If I’m missing something, I apologize. After several different attempts, and upon realizing I am able to query the collection through other methods with the same type of query - I figured this was a bug and not necessarily a user error. Mayhaps have missed something in the documentation too.
Like I said above, I went ahead and forked the repo so I can start trying to build some tests for it - if someone wants to point me in the right direction there for running those I’m happy to try and find the root of the issue as well.
Issue Analytics
- State:
- Created 10 years ago
- Comments:17 (1 by maintainers)
Top GitHub Comments
Bug found. User error (shocker).
Date('2013-07-29T20:15:09.000Z') !== new Date('2013-07-29T20:15:09.000Z')
. I was unaware there was a a difference between the two.Sorry to pester then, thanks much.
@sprjr : you’re rock bro
this work fine to me
Points.find({ createdAt: { $gt: new Date('2013-07-29T20:15:09.000Z'), } }, callback);
Thanks a lot