Home > Mobile >  Best practice to store price changes over time in Mongodb
Best practice to store price changes over time in Mongodb

Time:12-19

I'm using Nestjs and Mongoose as orm. I have a model called Product which has the following properties:

Product Model

    @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

    }

every 4 hours product price will get updated. I'm wondering what's the best way to store product price history in Mongodb. in a SQL database like PostgreSQL we make a ProductPrice table with (productId, price, date). But as mongodb max document size is 16MB and thousands of products that I have& what is the best solution in Mongodb??

CodePudding user response:

This is not challenging for MongoDB feel free and just create a ProductHistory collection for that:

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