Here we are first querying the db to get those documents having property lastupdated
const predicate = { lastupdated: { $exists: true, $type: "string" } }
const projection = { lastupdated: 1 }
const cursor = await mflix
.collection("movies")
.find(predicate, projection)
.toArray()
In the bulkwrite
stage we send the same document ids with updateOne
methods
const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({
updateOne: {
filter: { _id: ObjectId(_id) },
update: {
$set: { lastupdated: new Date(Date.parse(lastupdated)) },
},
},
}))
await mflix
.collection("movies")
.bulkWrite(moviesToMigrate)
So database needs to find the same document against the ids again.
Isn’t the database doing same work twice?