Home > Net >  MongoDB - Unable to add timestamp fields to subdocuments in an array
MongoDB - Unable to add timestamp fields to subdocuments in an array

Time:12-12

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!

CodePudding user response:

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",
                    one rror: {"error": "$$this._id"}
                  }
               }},
               "$$this"
            ]
          }
        }
      }
    }
  }
])
  • Related