Mongoose lean() is turning a number into a timestamp

Viewed 110

I have a prop called duration which is declared as a number in the mongoose schema with the purpose of storing a duration in seconds:

const mySchema = new mongoose.Schema(
    {
        ...
        duration: { type: Number, required: true }
        ...
    },
    { timestamps: true },
)

After using the method findOne() and applying the lean() method, the prop duration is returned as a timestamp when it was set as a number. It happens when the number is greater than 1000.

const myVideo = await Models.Video.findOne({ _id: videoId })
            .populate({ path: 'segment', populate: { path: 'type' } })
            .lean()
            .exec()

When I set: { "duration": 6000 } I get: { "duration": "1970-01-01T00:00:06.000Z" }

WHAT I'VE TRIED SO FAR

Besides trying to find the source of the issue, this is what I tried in the code:

  • I tried upgrading the Mongoose version from 5.9.15 to 5.12.7 to see if a fix was added for this but nothing changed.
  • Tried removing the { timestamp: true } from the schema, didn't work either.
  • Also tried adding other props or options like { lean: true } but at the end the result wasn't that different because I did stopped getting the timestamp but the returned object was a mongoose document instead of a plain old javascript object.

MY TEMPORARY SOLUTION

The temporary solution that I found for this was removing the lean() from the chain, but I still couldn't understand what was causing this.

0 Answers
Related