I am currently developing a solution in MongoDB, where i have two different data schemas in the same collection. The problem is that some of the data has different field names. For example:
In one of the contracts a date is named: date_at, but in another type of contract it is just named date.
So the question is: How do i sort on one field, and if it is not present in the document, sort on the other instead?
Currently i am trying to make a query such as this:
db.collection('contracts').aggregate([
{$project: {
date_at: [ $ifNull: ['$date', null]
}},
{$sort: { date_at: -1 }}
]);
But it doesn't seem to work. Hopefully one of you will be able to help me. Thank you in advance.