我想存储一组嵌套的子文档,例如下面的那个:
[
{"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);