Ticket: Migration - Can there be a better solution?

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
      .find(predicate, projection)

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

So database needs to find the same document against the ids again.

Isn’t the database doing same work twice?

Which work is being done twice?

The first find looks for document with lastupdated field of wrong type. The second operation update specific documents with the correction.

With the aggregation framework, it is probably possible to do it in one database access. But this is outside the scope of this course. M121 covers the aggregation framework.