在Mongodb中存储价格随时间变化的最佳实践



我使用NestjsMongoose作为ODM。我有一个名为"产品"的模型,它具有以下属性:

产品型号

@Schema({ timestamps: true, id:true, toJSON:{virtuals:true} })
export class Product {
@Prop({ required: true, type: String })
title: string
@Prop({ type: String })
brand: string
@Prop({ type: String })
description: string

@Prop({ type: Number})
currentPrice: number
}

每4小时产品价格将更新一次。我想知道在MongoDB中存储产品价格历史记录的最佳方式是什么。在像PostgreSQL这样的SQL数据库中,我们使用(productId,price,date(创建一个ProductPrice表。但由于CCD_ 5的最大文档大小是16MB;MongoDB的最佳解决方案是什么??

这对MongoDB来说并不具有挑战性,只需为其创建一个ProductHistory集合即可:

{
id: ObjectId,
product_id: ObjectId,
created_at: Date,
price: 99,
currency: "$",
expiration: 4,
expired: false
}

最新更新