Unable to update the layout for sub document definitions (mongodb)

Hey guys!

As posted in some other(s) topic(s), I have a couple of collection(s) (mongodb) but for some of them, I have documents like this:

Note: I will only show an “interface” approche, but schema would result to be the same, don’t worry, we keep it simple.

item.ts

import { Schema, Document, model} from 'mongoose';
import * as notes, {INotes} from './notes.ts';

interface IItem extends Document {
  notes: [INote],
  // ...
}

const schema = new Schema({
  'notes': [notes]
  // ...
}, {
  timestamps: false,
});

export default model<IItem>('items', schema, 'Items');

So, note is basically a simple model which isn’t a collection itself and used only as nested document schema.

note.ts

import { Schema, Document, model, Types} from 'mongoose';

export interface INote extends Document {
  title: string,
  message: string,
  author_id: Types.ObjectId
}

const schema = new Schema({
  "title": String,
  "message": String,
  'author_id': { type: Types.ObjectId, ref: 'xxx' },
}, {
  timestamps: false,
});

export default model<INote>('notes', schema);

The thing is, I am able to edit the layout from Forest Admin UI of the items (the collection display, the single item fields, etc) but I am not able to edit the fields types.

By example, one of the field is an author ID and, instead of a “text input”, I would like to put a Dropdown because I am in the same situation as I was in this topic: Dropdown(s) of constants/foreign keys on create/update from UI - #3 by Emixam23 @Arnaud_Moncel @remi.

I searched but couldn’t find the way to edit the “widget” from “text input” to “dropdown” when it’s a nested document.

I am close to be done, it’s amazing how fast you can build a backoffice using forest admin when you don’t have much skills in web dev!

Thanks!

Max,

Hello @Emixam23 and sorry for the late response,

To change a text input field into a dropdown you can simply do it via the layout editor. This little video illustrates how to do it.

Let me know if it helps! :slight_smile:

Hey :slight_smile:

Thanks for your answer, sadly, it doesn’t help as my situation is different :confused:

Try to have a collection with the configuration explained above, and try to edit the subdocument “Note”.
In the “Related Data” section, your notes will appear once you are on an item.

Try to edit the layout, you won’t be able to change the “Edit Widget”. It does work for the document, but not for sub documents (e.i. : the layout edition works on Item, but not on Note)

exemple with a Item document containing an array of notes:

{
  "_id": ObjectId("xxx"),
  "name": "My first note",
  "notes": [
    {
      "title": "My first note",
      "message": "We are currently trying to reproduce a situation",
      "author_id": ObjectId("yyy"),
      "priority": 2
    }, {
      "title": "My first note",
      "message": "We are currently trying to reproduce a situation",
      "author_id": ObjectId("yyy"),
      "priority": 3
    }
  ]
}

This note has a “priority” field as you can see. However, I want it to be a dropdown of “constants” which are staticly defined. I already have done it on Item, so I know how to do it and it works perfectly. But now, I want to do it on this nested document too, but as you can see (if you reproduce the issue, it’s not possible… If Note was a "foreign object) linked throught a foreing key, it wouldn’t be a nested document anymore “as it would have its own collection” so I would be able to create this dropdown using your technic on the video. But here, it’s a nested document, not a document from another collection.

Hope it helps you to better understand! :slight_smile:

Thanks,

Max

Hi @Emixam23 :wave: unfortunately it is not possible yet. The actual behavior of nested document today is:

  • the first level estimate sub field type and put default widget on it.
  • the second level just set a json editor widget to edit it.

So I will push your request on our product board.

2 Likes

Hi, any updates on supporting nested documents?

Hello @Shem ! Welcome to our community :tada: :confetti_ball:

We have a new feature called flatten nested fields available for mongodb but we don’t support edit of arrays of embedded fields. You can find more information here :arrow_down:

Let me know if that was what you were looking for.

Best regards,
Morgan

Yes, I know the flatten nested fields option, but it’s not helping me.
I have a mongo object that looks something like that:

doc: {
  _id: string,
  items: [
    name: string,
    subItems: [
      prop1: string,
      prop2: string,
    ]
  ]
}

So no way to have a decent view / edit capabilities for items or subItems

Hey again,

Indeed as I mention we don’t support, in a clean way, the display/edit of subDocuments embedded fields.

We have some work ongoing on this subject. I’ll push again the request on our product board so we can tackle it in a near future.

Kind regards,
Morgan

It’ had been two years guys. Is that too hard to support this feature?