0

I recently updated my subschemas (called Courses) to have timestamps and am trying to backfill existing documents to include createdAt/updatedAt fields.

Courses are stored in an array called courses in the user document.

// User document example

{
name: "Joe John",
age: 20,
courses: [
    {
      _id: <id here>, 
      name: "Intro to Geography", 
      units: 4
    } // Trying to add timestamps to each course
  ]
}

I would also like to derive the createdAt field from the Course's Mongo ID.

This is the code I'm using to attempt adding the timestamps to the subdocuments:

db.collection('user').updateMany(
    {
      'courses.0': { $exists: true },
    },
    {
      $set: {
        'courses.$[elem].createdAt': { $toDate: 'courses.$[elem]._id' },
      },
    },
    { arrayFilters: [{ 'elem.createdAt': { $exists: false } }] }
  );

However, after running the code, no fields are added to the Course subdocuments.

I'm using mongo ^4.1.1 and mongoose ^6.0.6.

Any help would be appreciated!

1
  • $toDate is an aggregation function, you can use it only in an aggregate but not in updateMany Commented Dec 11, 2021 at 6:40

1 Answer 1

0

Using aggregation operators and referencing the value of another field in an update statement requires using the pipeline form of update, which is not available until MongoDB 4.2.

Once you upgrade, you could use an update like this:

db.collection.updateMany({
  "courses": {$elemMatch: {
        _id:{$exists:true}, 
        createdAt: {$exists: false}
   }}
 },
 [{$set: {
      "courses": {
        $map: {
          input: "$courses",
          in: {
            $mergeObjects: [
              {createdAt: {
                  $convert: {
                    input: "$$this._id",
                    to: "date",
                    onError: {"error": "$$this._id"}
                  }
               }},
               "$$this"
            ]
          }
        }
      }
    }
  }
])
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed, thanks so much! I've learned a lot more about Mongo today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.