I am creating an aggregate query, but I'm running into an issue with some of my fields that need to be converted to ObjectIDs'. There are certain documents that do not have anything in the fields(null, ''), so I want to ignore those cases.
{
"$project": {
"Company": {
"$toObjectId": "$Company"
},
"Lease": {
"$toObjectId": "$Lease"
},
"Well": {
"$toObjectId": "$Well"
}
}
I've tried something similar to this:
{$ifNull: [{ $toObjectId: "$Company" }, ''] }
But I'm still receiving the error.
What is the best way to fix this?
I realized I was using the short syntax of $toObjectId, which didn't have the options of onNull, or onError.
I needed to revert back to $convert:
{$convert: {input: '$Company', to : 'objectId', onError: '',onNull: ''}}
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With