3

I am struggling to understand the populate method in Mongoose, while having trouble populating fields from one Mongoose model to another. The first model Schema is:

var MprnSchema = new mongoose.Schema({
    mprNo: {type: Number, unique: true, required: true},
    siteName: String,
    buildingNo: Number,
    streetAddress: String,
    secondAddress: String,
    townCity: String,
    postCode: String,
    supplier: String,
    siteContactName: String,
    siteContactNo: String,
});

module.exports = mongoose.model("Mprn", MprnSchema);

And another Schema is:

var FaultSchema = new mongoose.Schema({
    jobRef: Number,
    mprNo: Number,
    requestedDate: {type: Date, default: Date.now},
    attendedDate: {type: Date, default: null},
    siteDetails: {type: mongoose.Schema.Types.ObjectId, ref: 'Mprn'},
    faultIssue: String,
});

However, when I try to populate the 'siteDetails' with the contents of Mprn, I get [] when using thhe code below:

  Fault.find(reportQuery).populate('siteDetails', 'siteName 
postCode').exec((err, faultResults) => {
        if(err){
              req.flash("error", err.message);
              res.redirect("/");
        }

        console.log(faultResults);
        res.render("reportResults", { queryResults: faultResults });   

        });

The object passed to be rendered (queryResults) only contains the Fault data and NOT the 'siteDetails' that should be populated. I think the problem may be when I am using the Fault.Create() method in the first place, should I be saving a reference to the MPRN model at this point, so that the there is an Id to be referenced when populating?

My samples collections are:

Fault

  {
    "jobRef": 60000,
    "mprNo": 123456,
    "faultIssue": "Test"
  }

Mprn

 {
    "mprNo": 123456,
    "siteName": "Smithson Gates",
    "buildingNo": 76,
    "streetAddress": "Garden Place",
    "secondAddress": "Greater Manchester",
    "townCity": "Salford",
    "postCode": "M5 3AP",
    "supplier": "test",
    "siteContactName": "Mr B Jones",
    "siteContactNo": "0161 000 0000"
 }
ASTIN77
  • 125
  • 8
  • can you post your sample collection? – Ashh Jul 29 '18 at 14:48
  • I have added the collection data that I am testing this with. These Schemas are actually larger and I have truncated them to a minimum and have added the test data that is applicable. – ASTIN77 Jul 29 '18 at 15:35
  • which is the common (matching key) in both the collections? and what is your mongodb version? – Ashh Jul 29 '18 at 15:40
  • Apologies, the key is mprNo that is matching in each collections ( i have updated the mprNo value) and the MongoDB version is V4.0.0 – ASTIN77 Jul 29 '18 at 15:43

1 Answers1

0

Well you are using latest version of mongodb... So, You can try below $lookup aggregation... and the plus point of using this is you don't need to store _id of Mprn collection in the Fault... You can easily join both the collections with mprn number

Fault.aggregate([
  { "$match": reportQuery },
  { "$lookup": {
    "from": Mprn.collection.name,
    "let": { "mprNo": "$mprNo" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$mprNo", "$$mprNo" ] } } }
    ],
    "as": "siteDetails"
  }},
  { "$unwind": "$siteDetails" }
])
Ashh
  • 44,693
  • 14
  • 105
  • 132
  • Thanks, but I am not sure how I would use this within an express/Node route? – ASTIN77 Jul 29 '18 at 15:53
  • this is an aggregate query just like a find query of mongodb and also the alternate of populate query... just replace this with your find().populate() query it will work as same and give your desired result... you will learn a lot of things about mongodb after using this query... – Ashh Jul 29 '18 at 15:55
  • Thanks for all your help, but now I am getting db is not defined, should i be replacing **db** with something else? I am having severe brain-freeze today... – ASTIN77 Jul 29 '18 at 16:03
  • It should be `Fault`... And `Mprn.collection.name` is the name of your `Mprn` collection... you need to import it just you have imported `Fault` – Ashh Jul 29 '18 at 16:05
  • Thanks it worked! I am going to go an investigate aggregates in more depth now. Thanks for all your help! – ASTIN77 Jul 29 '18 at 16:09