1

In this case, I need to assign multiple Many-to-One relationships, as you can see below:

var portfolioSchema = new Schema({
    volatility         :  Number,
    bonds              :  {
        percentage          :  { type: Number, default: 0.6 },
        USA                 :  {
            percentage           :  { type: Number, default: 0.7 },
            treasury             :  {
                percentage            :  { type: Number, default: 0.4 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            },
            municipal            :  {
                percentage            :  { type: Number, default: 0.2 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            },
            corporate            :  {
                percentage            :  { type: Number, default: 0.4 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            }
        },
        international       :  {
            percentage           :  { type: Number, default: 0.3 },
            developed            :  {
                percentage            :  { type: Number, default: 0.7 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            },
            emergent        :  {
                percentage            :  { type: Number, default: 0.3 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            }
        }
    },
    stocks          :  {
        percentage          :  { type: Number, default: 0.4 },
        USA                 :  {
            percentage           :  { type: Number, default: 0.7 },
            largeCaps            :  {
                percentage            :  { type: Number, default: 0.4 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            },
            mediumCaps           :  {
                percentage            :  { type: Number, default: 0.4 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            },
            smallCaps            :  {
                percentage            :  { type: Number, default: 0.2 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            }
        },
        international       :  {
            percentage           :  { type: Number, default: 0.3 },
            developed            :  {
                percentage            :  { type: Number, default: 0.7 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            },
            emergent        :  {
                percentage            :  { type: Number, default: 0.3 },
                securities            :  [ { type: ObjectId, ref: 'Security' } ]
            }
        }
    },
    _goal           :  { type: ObjectId, ref: 'Goal' }
});

var securitySchema = new Schema({
    name               :  { type: Date, required: true },
    avgReturn          :  Number,
    avgRisk            :  Number,
    percentage         :  Number
});

However, It seems to me that it would be easier to simply use an Array data type instead of [ { type: ObjectId, ref: 'Security' } ].

I am not sure about what I should do. Anyone would know the appropriate way to do this? Thank you!

Stennie
  • 63,885
  • 14
  • 149
  • 175
bmpasini
  • 1,503
  • 1
  • 23
  • 43

2 Answers2

1

If you arrays are going to be only composed by your Security objects, you should use [ { type: ObjectId, ref: 'Security' } ].

0

You should not write the word Array in mongoose schemas. Here is a relevant answer. How to define object in array in Mongoose schema correctly with 2d geo index

So, the short answer to your question is: Use [ { type: ObjectId, ref: 'Security' } ].

Community
  • 1
  • 1
Vasyl Boroviak
  • 5,959
  • 5
  • 51
  • 70
  • I'm sorry, but the answer you said is relevant doesn't help me. It suggests using `Arrays` instead... Why would you suggest using `[ { type: ObjectId, ref: 'Security' } ]`? Could you please be more specific? – bmpasini Mar 18 '15 at 18:50