2

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);
lucasbbs
  • 349
  • 4
  • 14
  • In mongoose field name should be pre defined. That means "2021-02-01income" field cannot have dynamic field name – Pranavan Jul 31 '21 at 04:32
  • Ok, @Prana ... I understood, but maybe is there any other solution... because I need to store a object Id ref property when it happens to be a fund, i.e. the transaction field... What happens is that as I was tryng to save that before as simpe objects array I could not retrieve the already existing object Ids as such... and I cannot redeclare them using the function mongoose.Types.ObjectId(), because they already exist in the database :/ I mean I want to update that data... Do you got what I mean? What can I do?? Is there any workaround? – lucasbbs Jul 31 '21 at 04:41
  • Ok, I considered your key in the incomeSchema. Do you need to keep the same format or do you flexible with the format? – Pranavan Jul 31 '21 at 04:55
  • I need to keep the same format for the object Ids when retrieving the data to be updated – lucasbbs Jul 31 '21 at 05:05
  • That's not an issue. Issue is with the object inside incomeSchema array. You specified the key as a dynamic value. '2021-02-01income', '2021-03-01income', .... You can't have a dynamic named keys. – Pranavan Jul 31 '21 at 05:07
  • Hello, @Prana... I've solved my problem... Check the answer out – lucasbbs Jul 31 '21 at 05:40

1 Answers1

0

I found a workaround.... I just setted the _id in the Transaction schema as string and now everything is working... I mean I can update the array of neste subdocuments

lucasbbs
  • 349
  • 4
  • 14