简体   繁体   中英

How do i declare a dynamic key in an object for a Mongoose Schema

I have a structure like this

alerts: {
[symbol: string]: [
    {
     alert_price: number,
     price_when_set: number,
    }
  ]
},

how do i declare the above in a mongoose schema as the key value for the object is variable. Can i just use alerts: [Object] or alerts: [Schema.Types.Mixed] and use all kinds of objects within? or is there a better solution.

Thanks in advance!

Well after a lot of testing, I figured that that was indeed the solution. You can just declare the path as a Mixed type by either declaring it as on of the following:

  1. Schema.Types.Mixed
  2. Object
  3. Mongoose.Mixed
  4. {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM