question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aggregate $match with ISODate

See original GitHub issue

Hey 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:closed
  • Created 10 years ago
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
whichsteveypcommented, Aug 1, 2013

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.

5reactions
n0impossiblecommented, Oct 19, 2016

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to filter $match equals to Isodate using aggregate query ...
I would like to Filter aggregate with $match equals to currDate. i am using the below query in mongoose but i didn't get...
Read more >
How to search date range in aggregate - MongoDB
The group part work but I can't add the date search like in find, I've read a lot about ISODate but keep the...
Read more >
How to match date with MongoDB $match? - Tutorialspoint
To match date, use $match along with aggregate(). Let us create a collection with documents − > db.demo491.insertOne({"ShippingDate":new ...
Read more >
Stupid date tricks with Aggregation Framework
How do you insert ISODate without turning it into a string? I'm using the native mongodb driver in node, and that seems to...
Read more >
Best way to show dates out of $dayOfYear in Aggregation?
Since you can't do aggregation on more than one "_id" (key space) the thing to do would be to aggregate on the ISODate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found