You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Aggregation match doesn't seem to work with date value #4678
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.
FWIW, in my debugging last night (if I remember correctly) MongoStorageAdapter.js was converting a Date object into a string, and the mongodb adapter did not seem to like that. Tonight I'll modify the code to keep it a Date object and see if that makes a difference.
Yes, this was definitely it. When I converted the strings to new Date() and passed that on then I started getting results back. Within MongoStorageAdapter.js within the aggregate method I added check on schema.fields[field] to see if it was a date. If so, I convert the string into a Date object. That seems to do the trick. I can make a PR if you'd like to review the code.
I have the same issue, and with custom fields that are created by the aggregation pipeline, and not in the base Schema, the dates are not detected.
Modifying the _parseAggregateArgs method in the MongoStorageAdapter file to add a
} else if (pipeline instanceof Date) {
return pipeline;
}
condition before the if (typeof pipeline === 'object') seems to work, but I'm not sure of the repercussion of this.
Anyway, testing the schema in all the pipeline is weird as all the fields can be changed/removed/added at every stage, so the schema is not coherent anymore.
As for the isPointerField condition which is set during any $group stage and used to change the final $_id of the last stage of the pipeline... I just don't understand why, but it results in weird _id deletion (but I'm far from understanding all that code) 😅
Activity
dplewis commentedon Mar 26, 2018
Thanks for reporting. I'll have a look.
sauravazad commentedon Mar 27, 2018
I have been facing the same issue and it is related to only Date fields , number works fine.
eleynes commentedon Apr 6, 2018
Any updates on this? TIA
fmendoza commentedon Apr 16, 2018
Same here
eleynes commentedon Apr 17, 2018
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 });
}
});
});
dplewis commentedon Apr 17, 2018
Sorry guys been off the grid.
I'll submit a PR sometime this week.
cjbland commentedon Apr 30, 2018
FWIW, in my debugging last night (if I remember correctly)
MongoStorageAdapter.jswas converting aDateobject into a string, and the mongodb adapter did not seem to like that. Tonight I'll modify the code to keep it aDateobject and see if that makes a difference.cjbland commentedon Apr 30, 2018
Yes, this was definitely it. When I converted the strings to
new Date()and passed that on then I started getting results back. WithinMongoStorageAdapter.jswithin theaggregatemethod I added check onschema.fields[field]to see if it was a date. If so, I convert the string into aDateobject. That seems to do the trick. I can make a PR if you'd like to review the code.src/Adapters/Storage/Mongo/MongoStorageAdapter.js
flovilmart commentedon Apr 30, 2018
Go ahead with the PR! That sound like a good fiz
cjbland commentedon Apr 30, 2018
PR is GH-4743 let me know if you have any other improvements.
korhanozbek44 commentedon May 20, 2018
Same bug for me.
#4678: Converting strings to Date when schema.type is Date within agg… (
dplewis commentedon Jun 26, 2018
Closed via #4743
#4678: Converting strings to Date when schema.type is Date within agg… (
#4678: Converting strings to Date when schema.type is Date within agg… (
anhnhv commentedon Nov 18, 2018
It's still have a bug with this code:
query.aggregate([{match: {customDateField:{$exists:false}}}])I have edited the source code of MongoStorageAdapter.js to debug
if (stage.$match) { console.log(stage.$match); stage.$match = this._parseAggregateArgs(schema, stage.$match); console.log(stage.$match); }And what I received are:
{ customDateField: { $exists: false } }{ customDateField: { $exists: {} } }SebC99 commentedon Nov 7, 2019
I have the same issue, and with custom fields that are created by the aggregation pipeline, and not in the base Schema, the dates are not detected.
Modifying the
_parseAggregateArgsmethod in the MongoStorageAdapter file to add acondition before the
if (typeof pipeline === 'object')seems to work, but I'm not sure of the repercussion of this.Anyway, testing the schema in all the pipeline is weird as all the fields can be changed/removed/added at every stage, so the schema is not coherent anymore.
As for the
isPointerFieldcondition which is set during any$groupstage and used to change the final$_idof the last stage of the pipeline... I just don't understand why, but it results in weird_iddeletion (but I'm far from understanding all that code) 😅parse-community#4678: Converting strings to Date when schema.type is …
adrianaxente commentedon Jul 3, 2020
There is another work-around :
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.
ilovett commentedon Aug 10, 2020
Thanks for the workaround @adrianaxente
ansu16281 commentedon Feb 9, 2023
Here is the new syntax for Parse v6 +
It aggregates rows by date and counts the number of occurences within a date.
Not that unlike in previous versions you use _id instead of objectId, and you must prefix the stages with $ just like directtly in MongoDB