Flatten nested fields (MongoDB)
This feature is available from
[email protected]
Forest Admin introspects your data structure recursively, so nested fields (object in object) are detected at any level deep.
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 flattenHere is the way to declare the fields to flatten.
You can only target root-level sub-documents
Name | Type | Description |
---|---|---|
fieldsToFlatten | string[] or {field, level}[] |
|
Let's imagine a collection...
module.exports = (mongoose, Mongoose) => {
const schema = Mongoose.Schema({
// root-level
'id': Number,
'name': String,
'contactDetails':{
//Level 1
'phone': {
//Level 2
'homePhone': String,
'mobilePhone': String,
},
'email': String,
},
}, {
timestamps: false,
});
return mongoose.model('users', schema, 'users');
};
...where we can flatten
contactDetails
as followsFull depth flattening
Specific level flattening
forest/users.js
import { collection } from "forest-express-mongoose";
collection('users', {
actions: [],
fields: [],
segments: [],
fieldsToFlatten: ['contactDetails']
});
forest/users.js
import { collection } from "forest-express-mongoose";
collection('users', {
actions: [],
fields: [],
segments: [],
fieldsToFlatten: [{field: 'contactDetails', level: 0}]
});
If any field of a sub-document contains a reference, it will automatically be transformed into a
belongsTo
relationship.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.
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:
index.js
import { requestUnflattener } from 'forest-express-mongoose';
...
app.use('/forest', requestUnflattener);
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.
Last modified 3mo ago