如何使用嵌套子文档制作数组的猫鼬模式

我想存储一组嵌套的子文档,例如下面的那个:

[
  {"2021-02-01income":{"value":37.95,"tax":0,"type":"income"}},{"2021-03-01income":{"value":38.25,{"2021-03-08fund": {"value":-78,"type":"fund","transaction":"610378deead56742a898443b"}},{"2021-04-01income":{"value":38.53,{"2021-07-01income":{"type":"income","value":134}},]

我想出了以下不起作用的模式,因为如您所见,对象数组基于唯一键嵌套对象... 有没有我可以尝试的解决方法:

const incomeSchema = mongoose.Schema({
  type: { type: String,required: true },value: { type: Number,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,user: {
      type: mongoose.Schema.Types.ObjectId,ref: 'User',account: {
      type: mongoose.Schema.Types.ObjectId,required: true,ref: 'account',broker: {
      type: mongoose.Schema.Types.ObjectId,ref: 'Broker',type: { type: String,rate: { type: String,indexer: { type: String,investment_date: { type: Date,due_date: { type: Date,initial_amount: { type: Number,accrued_income: { type: Number,default: 0 },taxes: { type: Number,{ timestamps: true }
);

const Investment = mongoose.model('Investment',investmentSchema);
b50409346 回答:如何使用嵌套子文档制作数组的猫鼬模式

我找到了一个解决方法......我只是将事务架构中的 _id 设置为字符串,现在一切正常......我的意思是我可以更新嵌套子文档的数组

本文链接:https://www.f2er.com/6602.html

大家都在问