Add timestamp to a new subdocument or subschema in mongoose

Viewed 2391

I have this document in mongo atlas

_id: 5f8939cbedf74e363c37dd86,
firstname: "Person",
lastname: "Person lastname",
sex: "Masculino",
age: "20",
birthDay: 2020-10-07T00:00:00.000+00:00,
vaccines: Array
 0:Object
  dose: Array
   _id: 5f8939cbedf74e363c37dd87
   vaccine:5f7023ad96f7ed21e85be521
   createdAt:2020-10-16T06:12:27.726+00:00
   updatedAt:2020-10-16T06:12:27.726+00:00
 1:Object
  dose:Array
  _id:5f893a9ca98e97188c93fea8
  vaccine:5f70259796f7ed21e85be523
 2:Object
  dose:Array
  _id:5f893acda98e97188c93fea9
  vaccine:5f7023ad96f7ed21e85be521

This is my mongoose schema

const mySchema = new Schema({
  firstname: {
    type: String,
    required: true,
  },
  lastname: {
    type: String,
    required: true,
  },
  sex: {
    type: String,
    required: true,
  },
  age: {
    type: String,
    required: true,
  },
  birthDay: {
    type: Date,
    required: true,
  },
  vaccines: [
    {
      type: new Schema(
        {
          vaccine: {
            type: Schema.ObjectId,
            ref: "Vaccine",
          },
          dose: Array,
        },
        { timestamps: true }
      ),
    },
  ],
});

every time I add a new person the vaccines array gets one new object with the timestamp as you can see, in my js file I use this code:

const addPerson = (person) => {
  const myPerson= new Model(person);
  return myPerson.save();
};

Then when I add a new vaccine for the same person this does not get the timestamp, I'm using this code for that:

const addPersonVaccine = async ({ params, body }) => {
  if (!params) return Promise.reject("Invalid ID");
  const vaccines = [body];
  const foundPerson = await Model.updateOne(
    {
      _id: params,
    },
    {
      $push: {
        vaccines: vaccines,
      },
    }
  );
  return foundPerson;
};

This is what my body inside vaccines array has: [ { vaccine: '5f72c909594ee82d107bf870', dose: 'Primera' } ]

The problem is that I have no results about the next timestamps, as you can see in my mongo atlas document:

1:Object
  dose:Array
  _id:5f893a9ca98e97188c93fea8
  vaccine:5f70259796f7ed21e85be523
 2:Object
  dose:Array
  _id:5f893acda98e97188c93fea9
  vaccine:5f7023ad96f7ed21e85be521

Is that the best way to implement timestamps in subdocuments or sub schemas?

I will appreciate your answers, thnks

1 Answers

You can use mongoose schema timestamps options to the inner schemas

const mongoose = require("mongoose");
    
    const forumSchema = new mongoose.Schema(
      {
        title: { type: String, required: true },
        biddings: [
          {
            type: new mongoose.Schema(
              {
                biddingId: String,
                biddingPoints: Number
              },
              { timestamps: true }
            )
          }
        ]
      },
      { timestamps: true }
    );
    
    const Forum = mongoose.model("Forum", forumSchema);
    
    module.exports = Forum;

for more Mongoose schema set timestamp on nested document

Related