parse-server: Aggregation match doesn't seem to work with date value

Issue Description

When using the new aggregate options with a match pipeline on a Date value, it silently fails with empty result. By the way, to match a createdAt or a updatedAt field, we have to use the db form _created_at / _updated_at, and I’m not sure it’s on purpose. Either way, we can’t query them with the standard $lt or $gt operator.

Steps to reproduce

query.aggregate([{match: {_created_at:{$lt:new Date()}}}]) always returns [] whereas query.aggregate([{match: {_created_at:{$exists:true}}}]) works perfectly.

Expected Results

Both results should be the same

  • Server

    • parse-server version (Be specific! Don’t say ‘latest’.) : 2.7.4
    • Localhost or remote server? Localhost
  • Database

    • MongoDB version: 3.4
    • Storage engine: WiredTiger
    • Localhost or remote server? Mongo Atlas

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

There is another work-around :

{
    'match': {
        "$expr": {
            "$lte": [
                "$some_date_field", 
                {
                    "$toDate": "2020-09-09T00:00:00.000Z" 
                }
            ]
        }
    }
}

Where “2020-09-09T00:00:00.000Z” can be used as string. Hope it helps someone till the fix will be available in the following releases.

Thanks for the workaround @adrianaxente

Sorry guys been off the grid.

I’ll submit a PR sometime this week.

extracting databaseController adapter is my workaround.

Parse.Cloud.define(“testMongoAggregate”, function(request, response) { // const { val1, val2 } = request.params; const { AppCache } = require(“parse-server/lib/cache”); // this refrenese only in cloud cloud const app = AppCache.get(process.env.APP_ID);

const schema = { // you don’t have to write all specifics here, but have to have fields object. fields: {} };

const start = moment.utc().startOf(‘day’); // Start of day const end = moment.utc().endOf(‘day’); // End of day

const pipeline = [ { $match: { _created_at: { $lte: new Date(‘2018-04-11T00:00:00.000Z’), $gte: new Date(‘2018-04-01T00:00:00.000Z’) }, } }, { $sort: { _created_at: -1 } }, { $group: { _id: “$_p_project” } } ];

app.databaseController.adapter .aggregate(“Table”, schema, pipeline) .then(result => { console.log(result); if (result) { response.success({ success: result }); } else { response.error({ error: true }); } }); });