Home > database >  MongoDB aggregation: FieldPath may not start with $
MongoDB aggregation: FieldPath may not start with $

Time:11-07

This MongoDB aggregation is failing:

Attendance.aggregate([
            { $match: { cohort_id: cohort_id} },
            { $unwind: "$absences" },
            {
                $group: {
                    _id: {
                        term: "$absences.term",
                        $function:
                            {
                                body: function (day) {
                                    return day.getDay();
                                },
                                args: ["$absences.formatted_date.day"],
                                lang: "js",
                            },
                    },
                    count: { $sum: 1 },
                },
            },
            { $sort: { count: 1 } },
        ])

with this error:

uncaught exception: Error: command failed: {
        "ok" : 0,
        "errmsg" : "FieldPath field names may not start with '$'. Consider using $getField or $setField.",
        "code" : 16410,
        "codeName" : "Location16410"
} with original command request: {
        "aggregate" : "attendances",
        "pipeline" : [
                {
                        "$match" : {
                                "cohort_id" : "61858e13dc5e0d1ce0238abd"
                        }
                },
                {
                        "$unwind" : "$absences"
                },
                {
                        "$group" : {
                                "_id" : {
                                        "term" : "$absences.term",
                                        "$function" : {
                                                "body" : function (day) {                                     return day.getDay();                                 },
                                                "args" : [
                                                        "$absences.formatted_date.day"
                                                ],
                                                "lang" : "js"
                                        }
                                },
                                "count" : {
                                        "$sum" : 1
                                }
                        }
                },
                {
                        "$sort" : {
                                "count" : 1
                        }
                }
        ],
        "cursor" : {

        },
        "lsid" : {
                "id" : UUID("b4505aa0-e65e-46cd-8e31-03e4ecdbfe3b")
        }
} 
...

Not the most helpful error message.

Where am I referencing a field name wrong? Looks like it's expecting a field name without $ somewhere, but I can't seem to find where.

I've seen similar posts about this error, but they generally have to do with $project and $sort which does not seem to be the problem here

Thank you!

CodePudding user response:

It considers $function as field name. I think it should be like this:

    {
        $group: {
           _id: {
           term: "$absences.term",
           day: {
                $function:  {
                    body: function (day) {
                       return day.getDay();
                    },
                    args: ["$absences.formatted_date.day"],
                    lang: "js",
                },
           },
           count: { $sum: 1 },
       },
    }

Is this a school homework? day.getDay() sounds to be a very simple function which should be available native in MongoDB Query Language.

CodePudding user response:

Found a solution that's simpler and that works:

Attendance.aggregate([
            { $match: { cohort_id: cohort_id} },
            { $unwind: "$absences" },
            {
                $group: {
                    _id: {
                        term: "$absences.term",
                        day: {
                            $dayOfWeek: "$absences.formatted_date.day"
                       },
                    },
                    count: { $sum: 1 },
                },
            },
            { $sort: { count: 1 } },
        ])
  • Related