Scuba Steve
Scuba Steve

Reputation: 1648

update all dates in collection using the value in each document

So I had an issue with some code where the I had a date that was not predictable. I've fixed the code, but I'd like to repair the dataset.

I need to take the date portion of a column and update it to drop the time portion (midnight is fine, or whatever the default is).

I have this code:

db.rawDayForceEmployeeStatus.update({}
    ,{
        $set:{"dateOfStateCapture": {$dateToString:
                                                {
                                                 format: "%Y-%m-%d",
                                                 date: "dateOfStateCapture"
                                                }}
            
        }
    }

I would like to wrap the '$dateToString' function inside a 'new ISODate( )' statement, but the system seems to not like that. "

I'm certain I'm just butchering the syntax here of what I'm trying to do. I'm pretty sure I'm setting date to be equal to the literal "dateOfStateCapture", and not the date I intend to point at.

How can I take the date in this field, modify it, and put it back where I want it?

EDIT:

This is where I'm at with my thinking. This still won't run, but I'm hoping this expresses what I'm trying to do:

db.rawDayForceEmployeeStatus.updateMany({},
                                    {$set:
                                        {  
                                            "dateOfStateCapture": new ISODate(
                                             {$dateToString: 
                                                  { format: "%Y-%m-%d", 
                                                    date: "dateOfStateCapture"},
                                             }),
                                        }
                                    })

Upvotes: 1

Views: 822

Answers (1)

zswqa
zswqa

Reputation: 914

db.rawDayForceEmployeeStatus.updateMany(
  {},
  [
    { "$set": 
      {"dateOfStateCapture": 
        {"$dateToString":
          {
            format: "%Y-%m-%d",
            date: "$dateOfStateCapture"
          }
        }
      }
    }
  ]
)

$dateOfStateCapture $ to use field value

Upvotes: 1

Related Questions