I am currently trying to display the documents of a MongoDB database with their relations. As of now, I have the following:
User(s) → [Has outfit(s)] → Outfit(s) → [List of item(s)/product(s)] → Product(s)
We will, for the sake of this example, take only the Outfit model and the Product model. Here you can find two documents as a possible example (JSON Format):
// Outfit document
{
"_id" : ObjectId("5eeb712dfa3a7b9028787903"),
"picture_url" : "https://i.skyrock.net/3911/84063911/pics/3226492571_1_2_q8PvGEGD.jpg",
"products_ids" : [
ObjectId("5eeb712dfa3a7b9028787904"),
ObjectId("5eeb712dfa3a7b9028787905"),
ObjectId("5eeb712dfa3a7b9028787906"),
ObjectId("5eeb712dfa3a7b9028787907")
]
}
// Outfit Product Document
{
"_id" : ObjectId("5eeb712dfa3a7b9028787905"),
"outfit_id" : ObjectId("5eeb712dfa3a7b9028787903"),
"title" : "Chemise bucheron, rouge/noir, a carreaux",
"picture_url" : "https://www.projet13.com/9959-big_default/chemise-bucheron-rouge-noir-a-carreaux.jpg",
"price" : NumberLong(4200),
"redirect_url" : "https://www.projet13.com/6426-chemise-bucheron-rouge-noir-a-carreaux.html?gclid=Cj0KCQjwoaz3BRDnARIsAF1RfLfOJDZobj5gTT_uscskeyUqWgzZ_L2SJYvE_kMmk4weuzZpe-fc8cQaAr-IEALw_wcB#/10-taille-m/32-couleur-rouge"
}
Now, if we look at the forest admin part (model definitions), we have the following:
// Outfit Document
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');
// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({
'picture_url': String,
'products': [{ type: mongoose.Schema.Types.ObjectId, ref: 'outfitProductsCollection' }],
}, {
timestamps: false,
});
module.exports = mongoose.model('outfitsCollection', schema, 'OutfitsCollection');
// Outfit Product Document
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');
// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({
'outfit_id': { type: mongoose.Schema.Types.ObjectId, ref: 'outfitsCollection' },
'title': String,
'price': Number,
'redirect_url': String,
'picture_url': String,
}, {
timestamps: false,
});
module.exports = mongoose.model('outfitProductsCollection', schema, 'OutfitProductsCollection');
Expected behavior
Now that I have my belongsTo
and hasMany
setup, I do have the following behavior (see below images)
Outfit details
I have the details with a “link” to Products (which contains the related products)
Outfit Product details
I have the details of my product but not “link” to the related outfit
Actual behavior
I can’t retrieve the related document as it should.
Documentation links:
- HasMany → Relationships - Developer Guide
- BelongsTo → Relationships - Developer Guide
Failure Logs
I have no failure logs at all.
Context
Please provide any relevant information about your setup.
- Project Name: EyesApp
"dependencies": {
"chalk": "~1.1.3",
"cookie-parser": "1.4.4",
"cors": "2.8.5",
"debug": "~4.0.1",
"dotenv": "~6.1.0",
"express": "~4.16.3",
"express-jwt": "5.3.1",
"forest-express-mongoose": "^6.0.0",
"mongoose": "~5.8.2",
"morgan": "1.9.1",
"npm-watch": "^0.6.0",
"require-all": "^3.0.0"
}
We are currently plugged to MongoDB, no firebase implemented yet.