MongoDB
 SQL >> Base de Dados >  >> NoSQL >> MongoDB

Alterando tipo de string para data ignorando dados ausentes

Filtre strings vazias antes da conversão.

db.collection.updateMany(
  { $and: [ { startDate: { $type: "string" } }, { startDate: { $ne: "" } } ] },
  [ { $set: { startDate: { $dateFromString: { dateString: "$startDate", format: "%m/%d/%Y" } } } } ]
);