Objects of my collection have a field, that is an array of objects with one of the field being a string date
{
citizens: [{
name: 'John'
birthday: '1993/07/13'
},
{
name: 'Sarah'
birthday: '1996/07/13'
},
{
name: 'Natalia',
birthday: '2015/07/13'
}]
}
{
citizens: [{
name: 'Leo'
birthday: '1994/02/08'
},
{
name: 'Paul'
birthday: '1934/09/13'
},
{
name: 'Rego',
birthday: '2019/01/29'
}]
}
I want to set to all the users older than 18 status ‘adult’
Here is what I try to do:
users.updateMany({}, {
$set: { 'citizens.$[elem].status': 'adult' },
},
{
arrayFilters: [
{ 'elem.status': { $exists: false } },
{ $lt: [{ $toDate: 'elem.$birthday' }, 18yearsaAgoDate] }, <-- 18years don't mean much here, I actually use $$NOW
],
multi: true,
});
But I get ‘unknown top level operator: $lt‘ error when run this. How do I supposed to use $lt in arrayFilter?
Thanks in advance!
3
Answers
It would have worked like this if your date was already in the right format. Since you need to format it, I think you should use an aggregation pipeline with a $merge stage:
See how it works on the playground example
Here’s how you could do it in a simple update using the aggregation pipelined updates:
Mongo Playground
I’ve used some version 5+ operators like
$dateDiff
as it makes the code cleaner, but you could still achieve the same results without them using$subtract
and a constant for 18 years, like so:Mongo Playground
This is an update using the
arrayFilters
syntax.Note the date value
"2004/07/27"
is the day 18 years ago (very close approximate value). And using string values in date comparison requires that the value is formatted in "YYYY/mm/dd".