0

Hopefully this makes sense. Using mongoose to push new items into an array of subdocuments, the documents get added fine, however on certain (random) occasions, the createdAt date of the current subdocuments in the array all get changed to the most recently added timestamp. This just started happening recently and I cannot recreate the issue locally, however on production my clients (many users) are experiencing this.

My model is as follows, root shipment schema contains an subdocument array of documents (shipment documents) which are files that a user uploads to the shipment.

const DocumentFileSchema = new Schema({
    user: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            required: true
        },
        firstName: {
            type: String,
            required: true
        },
        lastName: {
            type: String,
            required: true
        }
    },
    file: {
        type: String,
        required: true
    },
    name: {
        type: String,
        required: true
    }
}, {
    timestamps: true
});

const ShipmentSchema = new Schema({
    ...
    documents: [DocumentFileSchema]
}, {
    timestamps: true,
    collection: 'shipments',
    toJSON: { virtuals: true }
})

I'm adding files using the following function (upload handled elsewhere). I recently added the createdAt date into this function to force a value instead of letting mongo handle the timestamp, and this has not solved the issue.

const addFiles = async (id, body, files) => {
    let user = JSON.parse(body.user);

    let docs = [];
    if (files) {
        files.forEach(v => docs.push({ user: { ...user }, name: v.key.replace('shipments/',''), file: withHttp(v.location), createdAt: new Date() }));
        return ShipmentModel.findByIdAndUpdate(
            { _id: id },
            { $push: { documents: { $each: docs } } },
            { new: true }
        );

    }
}

The record in mongo (locally) works flawless but as mentioned on production (which is the same mongo database), users are uploading documents at different times and the database ends up with all subdocument timestamps reset to the newest time.

What is does locally (correct, proper timestamps):

{
  "_id": {
    "$oid": "xxx"
  },
  ...
  "documents": [
    {
      "_id": {
        "$oid": "xxx"
      },
      "user": {
        "id": {
          "$oid": "xxx"
        },
        "firstName": "John",
        "lastName": "Doe"
      },
      "name": "VERSION 2-A.pdf",
      "file": "xxx.pdf",
      "updatedAt": {
        "$date": "2025-08-20T16:13:21.609Z"
      },
      "createdAt": {
        "$date": "2025-08-20T16:13:21.609Z"
      }
    },
    {
      "_id": {
        "$oid": "xxx"
      },
      "user": {
        "id": {
          "$oid": "xxx"
        },
        "firstName": "Jane",
        "lastName": "Doe"
      },
      "name": "PDF_Caregiver Connections-2.pdf",
      "file": "xxx.pdf",
      "updatedAt": {
        "$date": "2025-08-20T16:54:59.307Z"
      },
      "createdAt": {
        "$date": "2025-08-20T16:54:59.307Z"
      }
    }
  ],
  "createdAt": {
    "$date": "2024-07-23T18:05:50.052Z"
  },
  "updatedAt": {
    "$date": "2025-08-20T17:28:39.538Z"
  },
  "__v": 0
}

When another user adds a new document at a completely separate time this is what happens on production (but only happens randomly, not for every shipment), it resets createdAt date to most recent subdocument timestamp:

{
  "_id": {
    "$oid": "xxx"
  },
  ...
  "documents": [
    {
      "_id": {
        "$oid": "xxx"
      },
      "user": {
        "id": {
          "$oid": "xxx"
        },
        "firstName": "JOE",
        "lastName": "RANTSEVIC"
      },
      "name": "937379JR RC.pdf",
      "file": "xxx.pdf",
      "createdAt": {
        "$date": "2025-08-25T11:51:49.375Z"
      },
      "updatedAt": {
        "$date": "2025-08-25T11:51:49.375Z"
      }
    },
    {
      "_id": {
        "$oid": "xxx"
      },
      "user": {
        "id": {
          "$oid": "xxx"
        },
        "firstName": "ALAN",
        "lastName": "SALAZR"
      },
      "name": "RP-937379JR-Carrier-Contract.pdf",
      "file": "xxx.pdf",
      "createdAt": {
        "$date": "2025-08-25T11:51:49.375Z"
      },
      "updatedAt": {
        "$date": "2025-08-25T11:51:49.375Z"
      }
    }
  ],
  "createdAt": {
    "$date": "2025-08-25T11:15:00.627Z"
  },
  "updatedAt": {
    "$date": "2025-08-25T15:09:36.155Z"
  },
  "__v": 0
}

Please help!

3
  • Please see if somehow the subdocuments are overwritten while updating, if so, createdAt will be the latest. You may please read more here : mongoosejs.com/docs/timestamps.html#timestamps-on-subdocuments Commented Sep 3 at 6:03
  • According to documentation, $push and $each does not overwrite the entire array of subdocuments, and only timestamps for new subdocuments that are pushed into the array are modified. As mentioned, this happens randomly and not for all updates. Commented Sep 3 at 19:08
  • I don’t think setting createdAt manually will fix the issue. Have you tried removing timestamps: true from the schema and just setting the dates manually instead? Commented Oct 3 at 17:25

0

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.