Flatten nested fields (MongoDB)
Last updated
Last updated
Please be sure of your agent type and version and pick the right documentation accordingly.
This is the documentation of the forest-express-sequelize
and forest-express-mongoose
Node.js agents that will soon reach end-of-support.
forest-express-sequelize
v9 and forest-express-mongoose
v9 are replaced by @forestadmin/agent
v1.
Please check your agent type and version and read on or switch to the right documentation.
This feature is available from forest-express-mongoose@8.1.0
Forest Admin introspects your data structure recursively, so nested fields (object in object) are detected at any level deep.
By default, Forest Admin will use JSON widgets to show and edit this type of data.
In order to be able to display each field separately, you can flatten the sub-document.
Any flattening configuration will not affect your database structure, but only in-app data representation
In order to flatten a sub-document, you will first need to declare it in your code for a specific collection. Here we will flatten contactDetails
. There are two ways of declaring the field to flatten
Here is the way to declare the fields to flatten.
You can only target root-level sub-documents
Let's imagine a collection...
...where we can flatten contactDetails
as follows
Belongs to
If any field of a sub-document contains a reference, it will automatically be transformed into a belongsTo
relationship.
Has many
If any of the fields in the sub-document is an array of references, it will automatically be transformed into a hasMany
relationship and moved under Related data section.
As the flattened fields will be considered native fields, the searching, filtering, and sorting are automatically supported. You can disable it in the field's configuration.
You can build your segments using flattened fields as you would do with the native field. You can of course mix flattened and native fields in the segment definition.
You can scope your data on flattened fields.
You don't need to implement any specific logic for editing, Forest Admin will reconcile the data to the database format.
Every flattened field will appear in the application as an independent field, so you can configure the edit widget for it.
Routes
The flattener is intended to work automatically with the default routes (eg. the routes handled by default by Forest Admin which you have not overridden).
If you want to use the flatten feature with some custom routes, you will need to ensure that the data coming from outside of your server have been "reconciled", or in other word, that the flattener has translated the data sent from the web application into something corresponding to your database structure. To do so, you will need to activate the reconciliation mechanism on your own, by adding a simple middleware on top of your routes. Here is an example:
By doing so, every routes related to forest will first, reconcile the data sent by the web application, and dispose them to you with a format corresponding to your database structure you can work with.
Name | Type | Description |
---|---|---|
fieldsToFlatten
string[]
or {field, level}[]
it is possible to put the names directly - all the levels will be flattened
It is possible to specify the flattening level
field
- name of the field
level
- starts at 0