I want to store an array of nested subdocuments such as the one down bellow:
[
  {"2021-02-01income":{"value":37.95,"tax":0,"type":"income"}},
  {"2021-03-01income":{"value":38.25,"tax":0,"type":"income"}},
  {"2021-03-08fund": {"value":-78,"type":"fund","transaction":"610378deead56742a898443b"}},
  {"2021-04-01income":{"value":38.53,"tax":0,"type":"income"}},
  {"2021-07-01income":{"type":"income","tax":0,"value":134}},
  ]
I came up with the following schema which is not working, because as you can see the array of objects is based on unique keys nested objects... Is there any workaround I can try:
const incomeSchema = mongoose.Schema({
  type: { type: String, required: true },
  value: { type: Number, required: true },
  tax: { type: Number, required: false },
  transaction: {
    type: mongoose.Schema.Types.ObjectId,
    required: false,
    ref: 'Transaction',
  },
});
const investmentSchema = mongoose.Schema(
  {
    incomes: [{ type: incomeSchema }],
    name: { type: String, required: true },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      required: false,
      ref: 'User',
    },
    account: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: 'Account',
    },
    broker: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: 'Broker',
    },
    type: { type: String, required: true },
    rate: { type: String, required: true },
    indexer: { type: String, required: true },
    investment_date: { type: Date, required: true },
    due_date: { type: Date, required: true },
    initial_amount: { type: Number, required: true },
    accrued_income: { type: Number, required: false, default: 0 },
    taxes: { type: Number, required: false, default: 0 },
    
  },
  { timestamps: true }
);
const Investment = mongoose.model('Investment', investmentSchema);