Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter subdocuments where field does not exist

I have collection of objects where each object contains versions some of which have delete field and would like to filter these which were not deleted or later than the given timestamp.

db
.getCollection('test')
.aggregate([
{$project:
 {versions:
  {$filter:
   {input: '$versions',
    as: 'version',
    cond:
    {
        $or: [
        {$gte: ['$$version.delete', 3]},
        {'$$version.delete': {$exists: false}}
        ]
    }
   }
  }
 }
}
])

but field name probably can't be used inside filter because I get an error: invalid operator '$$version.delete'

Any ideas what is the proper syntax of this query?

like image 589
kamil herba Avatar asked Sep 14 '25 09:09

kamil herba


1 Answers

You want to use aggregation syntax for checking type of delete field and comparing it to missing which is what $exists:false would be:

{$eq:[{$type:'$$version.delete'},"missing"]}
like image 124
Asya Kamsky Avatar answered Sep 16 '25 12:09

Asya Kamsky