How to handle fields that doesn't exist or null in MongoDB aggregation

I have a collection wherein initially I have the following structure of user logged records

"_id": "5d6565236574b1162c349d8f",
        "name": "Sarah Gates",
        "department": "IT",
        "origin": "Texas",
        "employDate": "2019-08-27",
        "__v": 0,

And then, it is later updated like so :

 "_id": "5d6565236574b1162c349d8f",
        "name": "Sarah Gates",
        "department": "IT",
        "origin": "Texas",
        "employDate": "2019-08-27",
        "__v": 0,
        "attendances": {
            "2019-08-28": "Present"
        }
    },

How can I possibly get mongo to ignore this field before it is created? since as soon as the application mounts the total sum of “Present” in the last 30 days for all the records in the database is called

 "attendances": {
            "2019-08-28": "Present"
        }

and I figure because the field “attendances” doesn’t yet exist hence the error!

The application uses aggregation framework and vanilla javascript to get the record of the last 30 days and then sum the number of all “present” like so :

app.get('/api/employee/sumattendances',(req,res)=>{
  //sum attendances
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}


const listDatesForThePastDays = n => (
    Array(n)
        .fill(new Date())
        .map((today, i) => today - 8.64e7 * i)
        .map(formatDate)
)

    Employee.aggregate([
    { "$group": { 
        "_id": null,
        "totalPresent": {
            "$sum": { 
                "$size": {
                    "$filter": {
                        "input": { "$objectToArray": "$attendances" },
                        "cond": {
                            "$and": [
                                { "$in": ["$$this.k", listDatesForThePastDays(30)] },
                                { "$eq": ["$$this.v", "Present"] }
                            ]
                        }
                    }
                }
            }
        } 
    } }
]).exec((err, results) => { if (err) throw err; res.json(results); })
})


I really need someone who can help out, Thanks in advance

Hello!
Uhm, coudn’t you just insert a match stage before the group one to select only the documents where ‘attendances’ field exists? If found none it should return an empty set to the group stage and this would allow to skip the error if i am not mistaken ^^