Hello,
I am trying to relate several collections, but when applying the aggregate it sends me the combination of documents that have no relationship.
Is there a way to filter by 2 or 3 fields so that the result is more specific?
Example using only one field as a relationship:
db.Compras.aggregate([
{
$lookup: {
from: ‘Subsidiaria’,
localField: ‘come.idCo’,
foreignField: ‘come.idCo’,
as: ‘subsidiaria_info’,
},
},
{
$lookup: {
from: ‘Sucursal’,
localField: ‘idSucursal’,
foreignField: ‘subsidiaria_info.idSucursal’,
as: ‘sucursal_info’,
},
},
{
$lookup: {
from: ‘Usuario’,
localField: ‘idUsuario’,
foreignField: ‘tra.idUsuario’,
as: ‘Usuario_info’,
},
},
{
$lookup: {
from: ‘Persona’,
localField: ‘idPersona’,
foreignField: ‘Usuario_info.persona.idPer’,
as: ‘Persona_info’,
},
},
{
$unwind: ‘$subsidiaria_info’,
},
{
$unwind: ‘$sucursal_info’,
},
{
$unwind: ‘$Usuario_info’,
},
{
$unwind: ‘$Persona_info’,
}
]);
Your help would be very useful to me.