How to add extra attributes to mongoose js model while its referenced in other model (Mongoose js relationships)

I am new to Mongoose js and in general to NODE JS. In My application there are two models as follows

Test Case Schema

var TestCaseSchema = new Schema({
    description: String,
    expected: String,
    actual: String,
    result: String,
    createdAt: {type:Date},
    updatedAt: {type:Date, default: Date.now()},
});

And

Test Plan Schema as follows

var TestPlanSchema = new Schema({
    name: {type: String, required: true},
    createdAt: Date,
    updatedAt: {type: Date, default: Date.now()},
    createdBy: {type: Schema.Types.ObjectId, ref:'User'},
    testCases: [{type: Schema.Types.ObjectId, ref:'TestCase'}],
    result: String,
});

As you can see Test case schema is referred by Test Plan schema

testCases: [{type: Schema.Types.ObjectId, ref:'TestCase'}]

Question: Is there an easy way to store the test case result in Test Plan. ( I do not want to store the results against the test case model as one test case can exists in multiple test cases and this can lead to false data)

Any ideas/pointers

Many thanks in advance

Upvotes: 0

Views: 1193

Answers (1)

janzal
janzal

Reputation: 176

So what about:

var TestPlanSchema = new Schema({
    name: {type: String, required: true},
    createdAt: Date,
    updatedAt: {type: Date, default: Date.now()},
    createdBy: {type: Schema.Types.ObjectId, ref:'User'},
    testCases: [
        {
            testCase: { type: Schema.Types.ObjectId, ref: 'TestCase'},
            result: String
        }
    ],
    result: String,
});

You now may save additional info to each TestCase as part of TestPlan.

Upvotes: 3

Related Questions