Linking to another collection inside “Related Data”

Feature(s) impacted

I have the following mongoDB data structures:
export const ParticipantTagValueSchema = new Mongoose.Schema({
label: { type: String, required: true, trim: true },
order: { type: Number, required: true, min: 0 }
});

export const ParticipantTagNameSchema = new Mongoose.Schema({
label: { type: String, required: true, trim: true },
values: { type: [ParticipantTagValueSchema], required: true, default: },
order: { type: Number, required: true, min: 0 }
});
export const ConversationTagValueSchema = new Mongoose.Schema({
participantTagValue: { type: Mongoose.Schema.Types.ObjectId, required: true },
participantTagName: { type: Mongoose.Schema.Types.ObjectId, required: true }
});

Observed behavior

When I am viewing a ConversationTagValueSchema as related data for a Conversation, it shows the id as plain text rather than a link. All of these schemas are nested which is maybe the issue (ParticipantTagNameSchema is nested under a different collection)? I am using mongoose and flattenMode: ‘auto’

Failure Logs

Context

  • Project name: …
  • Team name: …
  • Environment name: …
  • Database type: …
  • Recent changes made on your end if any: …

And, if you are self-hosting your agent:

  • Agent technology: nodejs

  • Agent (forest package) name & version (from your .lock file):"

  • @forestadmin/agent": “^1.65.0”,

        "@forestadmin/datasource-customizer": "^1.59.1",
    
        "@forestadmin/datasource-mongoose": "^1.12.4",
    

I have also tried adding references to the schema but when I do and try to load the record I get an error, “Cannot read properties of undefined (reading ‘collection’)”

Is there any work around for being able to click to the referred to record?

Hi DrewB,

Thank you for sharing these schemas.
Primary, it sounds incorrect to have ‘objectId’ as a type in this part:

participantTagValue: { type: Mongoose.Schema.Types.ObjectId, required: true },
participantTagName: { type: Mongoose.Schema.Types.ObjectId, required: true }

Can you also please share which of those schemas are defined as models?

Depending on this, there are many possibilities:
If ParticipantTagValueSchema, ParticipantTagNameSchema and ConversationTagValueSchema are models, you have to use the “ref” field.
If not, instead of using “ObjectId”, the correct schema type is preferred:
participantTagValue: { type: ParticipantTagValueSchema, required: true },
participantTagName: { type: ParticipantTagNameSchema, required: true }

Best regards,
Slim

I am not sure why you say it is incorrect to have ObjectId. That is what is stored. If I do the schema then it will expect the full object not the Id. My models are you asking which are collections? None of them are. They are all schema that are nested in other collections which I assume is what makes this tricky. When I tried using “ref” for the collection that forest admin creates then I get the error, “Cannot read properties of undefined (reading ‘collection’)” when loading the page.

Hi DrewB,

This type of relationship is not possible with MongoDB; relationships with MongoDB should target collections. It’s because it’s impossible to discover which document contains the sub-document you want to target.

Please consider a relational database to address this kind of relationship.
Otherwise, please explain in more detail what the expected result is on the UI, including the collections containing the shared schemas.

Best regards