'How to find data with condition on a second collection with Mongoose

I have a collection "campanas" with similar structure to:

{
 _id: ObjectId,
name:  String,
starts: Date,
ends: Date,
available: Boolean,
max_requirements: Int,
}

Whenever a user interacts with the Campana a requirement is generated with this structure in a different collection called "campana_requrements":

{
_id: ObjectId,
id_campana: {type: ObjectId, ref: "campanas"},
id_user: {type: ObjectId, ref: "users",
action_needed: String
}

Campanas also has attributes that are stored in its own collection called "campanas_attributes"

{
_id: ObjectId,
attribute_name: String
}

and campana and attributes are related through a 3rd collection called "campana_campana_attribute"

  {
    _id: ObjectId,
    id_campana: {type: ObjectId, ref: "campanas"},
    id_campana_attribute: {type: ObjectId, ref:"campana_attributes"}
}

So campanas I need to retrieve Campanas with its attributes BUT filter out the ones which has requirements generated by the user, in pseudo code the condition would be

campanas.max_requirements < requirements.length WHERE requirements.id_campana = campana._id AND requirements.id_user = $user

where $user is a variable I would provide. I can easily find the campanas and add its attributes, but i cant find a way to filter depending on amount of requirements using only mongodb / mongoose methods.

I want to achieve this in the query, not with nodejs.

How can i achived this?


UPDATE

I almost got it working using Studio3T aggregation tool but the reference of "$max_requirements" fails on the $match statement, its not taking the value. It works like a charm if i hard code a 1 into it but it might not be a 1 in the future. Heres the aggregation:

// Requires official MongoShell 3.6+
campanas.aggregate(
    [
        { 
            "$lookup" : { 
                "from" : "tbl_campana_requirements", 
                "localField" : "_id", 
                "foreignField" : "id_campana", 
                "as" : "requirements", 
                "pipeline" : [
                    { 
                        "$match" : { 
                            "$or" : [
                                { 
                                    "id_user" : ObjectId("someId")
                                }, 
                                { 
                                    "id_user" : null
                                }
                            ]
                        }
                    }
                ]
            }
        }, 
        { 
            "$addFields" : { 
                "req_count" : { 
                    "$size" : "$requirements"
                }
            }
        }, 
        { 
            "$match" : { 
                "habilitado" : true, 
                "$or" : [
                    { 
                        "req_count" : { 
                            "$lt" : "$max_requirements"   <--this is the problem
                        }
                    }, 
                    { 
                        "max_requirements" : { 
                            "$eq" : 0.0
                        }
                    }
                ]
            }
        }, 
        { 
            "$lookup" : { 
                "from" : "tbl_campana_campana_atributos", 
                "localField" : "_id", 
                "foreignField" : "id_campana", 
                "pipeline" : [
                    { 
                        "$lookup" : { 
                            "from" : "tbl_campana_atributos", 
                            "localField" : "id_campana_atributo", 
                            "foreignField" : "_id", 
                            "as" : "atributo"
                        }
                    }, 
                    { 
                        "$project" : { 
                            "_id" : 0.0, 
                            "atributo" : "$atributo"
                        }
                    }, 
                    { 
                        "$unwind" : "$atributo"
                    }
                ], 
                "as" : "atributos"
            }
        }, 
        { 
            "$project" : { 
                "atributos" : "$atributos.atributo", 
                "habilitado" : 1.0, 
                "nombre" : 1.0, 
                "titulo" : 1.0, 
                "descripcion" : 1.0, 
                "fecha_inicio" : 1.0, 
                "fecha_fin" : 1.0, 
                "id_campana_responsable" : 1.0, 
                "url_imagen_banner" : 1.0, 
                "url_imagen_fondo" : 1.0, 
                "url_imagen_principal" : 1.0, 
                "condiciones_terminos" : 1.0
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source