Answer the question
In order to leave comments, you need to log in
How to group documents in MongoDB/mongoose?
There is the following model structure:
//Unit.js
var mongoose = require('mongoose');
module.exports = mongoose.model('Unit', new mongoose.Schema({
title: {type: String},
_folder: {type: mongoose.Schema.Types.ObjectId, ref: 'Folder'}
}));
//Folder.js
var mongoose = require('mongoose');
module.exports = mongoose.model('Folder', new mongoose.Schema({
title: {type: String}
}));
//Document.js
var mongoose = require('mongoose');
module.exports = mongoose.model('Document', new mongoose.Schema({
title: {type: String},
folders: [{type: mongoose.Schema.Types.ObjectId, ref: 'Folder'}]
}));
app.get('/document/:id', function(req, res, next) {
return Document.findOneById(req.params.id)
.populate('folders')
.exec(function (err, document) {
if (err) return next(err);
// здесь нужно наполнить массив document.folders соответствующими элементами (units)
});
});
{
_id: ObjectId(...),
title: 'Document title',
folders: [
{
_id: ObjectId(...),
units: [
{
_id: ObjectId(...),
title: 'Unit title'
}
]
}
]
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question