I use MongoDB native driver in my NodeJS application.
I have a shifts
collection in my database that I need to update. Sample docs in my shifts collection
{
"_id" : ObjectId("588425105560bd2ba0065fa4"),
"from" : ISODate("2017-01-23T03:20:00.000Z"),
"to" : ISODate("2017-01-23T06:20:00.000Z"),
"jobId" : ObjectId("586efda790541421b0432897"),
"hourlyRate" : 15
}
{
"_id" : ObjectId("588425105560bd2ba0065fa5"),
"from" : ISODate("2017-01-25T03:20:00.000Z"),
"to" : ISODate("2017-01-25T06:20:00.000Z"),
"jobId" : ObjectId("586efda790541421b0432897"),
"hourlyRate" : 15
}
What I need to do is the following -
Update the hourlyRate
of all docs that meet the conditions:
- match the jobId (that is easy)
- set
hourlyRate
= 20 iffrom
is a Weekday - set
hourlyRate
= 25 iffrom
is a Saturday - set
hourlyRate
= 30 iffrom
is a Sunday
I would want to do it in a single query as far as possible.
My solution so far:
Use switch case and determine the type of day using $dayOfWeek
from Date aggregation function. However, I am not able to combine switch with updateMany
.
Any help would be appreciated.