.aggregate() on 2 collections mongoose

Viewed 42

Here is the 1st Schema:

const ProSchema = new Schema({
_id: { type: Schema.Types.ObjectId },
Cv: {Fonction: {Pro: {type: Boolean,},
             Secretaire: {type: Boolean}
}
})

Here's the second Schema:

const CabSchema = new Schema({
Pro: [{ type: Schema.Types.ObjectId, ref: 'ProSchema' }],
InfoCab: {
    Nom: {type: String}
});

Creating a cabinet:

var cabinet = new cabModel({
_id: new mongoose.Types.ObjectId(),
InfoCab:{Nom: "Clinique Toto"} //This is the Name of the Cabinet
}); 
cabinet.save((err, cabinet) => {

Creating pro1:

var pro1 = new proModel({
    _id: new mongoose.Types.ObjectId(),
    Nom: 'ProTITI',
    Cv:{ Fonction: { Secretaire: false}}
});
pro1.Cabinets.push(cabinet._id);
pro1.save((err, cabinet) =>  { });

Creating pro2:

var pro2 = new proModel({
    _id: new mongoose.Types.ObjectId(),
    Nom: 'Pro_TOT',
    Cv:{ Fonction: { Secretaire: true}}
});
pro2.Cabinets.push(cabinet._id);
pro2.save((err, cabinet) =>  { });

Creating Pro3:

var pro3 = new proModel({
    _id: new mongoose.Types.ObjectId(),
    Nom: 'Josianne',
    Cv:{ Fonction: { Secretaire: true}}
});
pro3.Cabinets.push(cabinet._id);
pro3.save((err, cabinet) =>  { });

cabinet.Pro.push(pro1, pro2, pro3);
cabinet.save();

The purpose of this question is the following:

How can fetch a Pro of type Secretaire from Clinique Toto.

So far I tried with this:

db.Pro.aggregate([
{
    $match: {
        Cv: {
            Fonction: {
                Secretaire: true
            }
        }
    }
},
{
    $lookup:
    {
        from: "Cab",
         localField:"InfoCab.Nom",
         foreignField: "_id",
          as: "PK"
    }
}

But I'm stuck with the last bit: from Clinique Toto.

Cheers,

0 Answers
Related