How to reference the objectId from one collection/schema to another?

Hello everybody,
I have two schemas. The first looks as follows:

const TestRunSchema: Schema = new Schema(

    {

        testRun: {

            type: Array,

            testcases: [

                {

                    name: { type: String },

                    url: { type: String },

                    devices: { type: [String] },

                    userAgent: { type: String },

                    viewport: {

                        width: { type: Number },

                        height: { type: Number }

                    },

                    isMobile: { type: Boolean },

                    hasTouch: { type: Boolean },

                    browser: { type: String },

                    fullPage: { type: Boolean },

                    element: {

                        toScreenshot: { type: String },

                        toClick: { type: String },

                        toHover: { type: String }

                    }

                }

            ]

        }

    },

);

The second looks as follows:

const TestCaseSchema: Schema = new Schema([

    {

        testrunId: {

            type: mongoose.Schema.Types.ObjectId,

            ref: 'TestRun'

        },

        name: String,

        url: String,

        devices: [{type: String}],

        userAgent: {type: String},

        viewport: {

            width: {type: Number},

            height: {type: Number}

        },

        isMobile: {type: Boolean},

        hasTouch: {type: Boolean},

        browser: {type: String},

        fullPage: {type: Boolean},

        element: {

            toScreenshot: {type: String},

            toClick: {type: String},

            toHover: {type: String}

        }

    },

    {

        versionKey: false

    }

]);

You can interpret the TestRun as whole and the TestCases as subset of TestRun. When I’m saving the testrun into the database into the testrun collection the testcases will also be saved into the testcases collection as own object per testcase.
What I want to do is to add the objectId from the Testrun document into each testcase object or in other words I want to reference the objectId from the testrun to each testcase. Is it possible somehow?

From what I read so far I thought something like this could do the work.

testrunId: {

            type: mongoose.Schema.Types.ObjectId,

            ref: 'TestRun'

        },

But this didn’t work.

I’m a absolute beginner, so maybe I’m getting something totally wrong.

Any help is really appreciated.

Best regards

Hello @CHH_N_A ,

I notice you haven’t had a response to this topic yet - were you able to find a solution?
If not, could you please confirm if my understanding of your use case is correct?

You are trying to refer the objectId of documents of TestCases collection into documents of TestRun collection.
If this is correct then you can try using populate().

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single document, multiple documents, a plain object, multiple plain objects, or all objects returned from a query. Let’s look at an example.

const mongoose = require('mongoose');
const { Schema } = mongoose;

const personSchema = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  age: Number,
  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

const storySchema = Schema({
  author: { type: Schema.Types.ObjectId, ref: 'Person' },
  title: String,
  fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);

In case you have any more queries regarding this, then please provide some sample documents and example scenario to discuss further.

Regards,
Tarun

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.