Amine Harbaoui
Amine Harbaoui

Reputation: 1295

Mongoose: deep population

I have Patient model contains ref to Path :

const PatientSchema = Schema({
  idPatient: Schema.Types.ObjectId,
  firstName: String,
  lastName: String,
  path: {type: Schema.Types.ObjectId, ref: 'Path'}
});

Path model with array of ref to Zone:

const PathSchema = connection.mongoose.Schema({
  zones: [{
    type: connection.mongoose.Schema.Types.ObjectId,
    ref: 'Zone'
  }],
});

and finally Zone model:

const ZoneSchema = connection.mongoose.Schema({
  name: String,
  duration: Number,
});

I'm trying to get all the patient with their path and the zones for each path inside patient:

Here an example of data with a simple populate on the patient:

[
  {
    "_id": "5d00b7dab927301ad392e6e4",
    "idPatient": "5d00b7dab927301ad392e6e5",
    "firstName": "Amine",
    "lastName": "Harbaoui",
    "path": {
        "_id": "5d010263b927301ad392e6ea",
        "zones": [
            "5d010c72b927301ad392e6eb",
            "5d010cf7b927301ad392e6ec"
        ]
    }
  }
]

And here how I tried to get what I want:

Patient.find()
    .lean()
    .populate('path')
    .exec((error, patients) => {
      if (error) {
        console.log(error);
      } else {
        const zones = patients.map(p => p.path.zones);
        Path.populate(zones, {
          path: 'zones'
        }, (error, data) => {
          if (error) {
            console.log(error);
          } else {
            console.log(data);
          }
        })
      }
    })

But here's the exception I get:

{ MissingSchemaError: Schema hasn't been registered for model "Zone". Use mongoose.model(name, schema) at new MissingSchemaError (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/error/missingSchema.js:22:11) at NativeConnection.Connection.model (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/connection.js:888:11) at getModelsMapForPopulate (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/model.js:4337:57) at populate (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/model.js:3915:21) at _populate (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/model.js:3885:5) at utils.promiseOrCallback.cb (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/model.js:3858:5) at Object.promiseOrCallback (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/utils.js:248:12) at Function.Model.populate (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/model.js:3857:16) at Patient.find.lean.populate.exec (/home/amine/ubudu/app_mn/sprint0/server/controllers/patientController.js:61:14) at /home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/model.js:4733:16 at /home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/utils.js:263:16 at _hooks.execPost (/home/amine/ubudu/app_mn/sprint0/server/node_modules/mongoose/lib/query.js:4224:11) at /home/amine/ubudu/app_mn/sprint0/server/node_modules/kareem/index.js:135:16 at process._tickCallback (internal/process/next_tick.js:61:11) message: 'Schema hasn\'t been registered for model "Zone".\nUse mongoose.model(name, schema)', name: 'MissingSchemaError' }

Upvotes: 0

Views: 347

Answers (1)

Christian Colón
Christian Colón

Reputation: 106

As long as you are using one of the more recent versions of mongoose, you can populated deeply nested values in one query.

Patient.find()
.populate({
    path: 'path',
    populate: {
       path: 'zones'
    }
}).exec()

Upvotes: 0

Related Questions