1

I have an array of objectIDs references in mongo. I want to get a specific element in that array after populating the objectIDs. the problem is i get an empty array. Here's my schema

// Patient Schema - start
const patientSchema = new mongoose.Schema({
    nom: {
      type: String,
      required:true
    },
    prénom: {
      type: String,
      required:true
    },
    naissance:{
      type:Date,
    },
    adresse: {
      type: String,
      required:true
    },
    téléphone: {
      type: String,
      required:true
    },
    profession: {
      type: String,
    },
    /// the field i'm trying to populate 
    consultations:[{
      type: mongoose.Schema.Types.ObjectId,
      ref:'Consultation'
    }],
    salle:{
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref:'Salle'
    },
    date:{
      type:String,
      default: Date.now
    },
    jointes: {
      type:Array
    },
    questionnaire: {
      type:Object
    },
  }, { collection : 'patients'} );
  
  const patients = mongoose.model('Patient', patientSchema);

Consultation schema

 const consultationSchema = new mongoose.Schema({
      date: {
        type: String,
        required:true
      },
      motif:{
        type: String,
      },
      observations: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Observation"
      }],
      paiements: [{
            type: mongoose.Schema.Types.ObjectId,
            ref: "Paiement"
      }],
      ordonnances: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Ordonnance"
      }]
    });
 const consultations = mongoose.model('Consultation', consultationSchema);

the exports

  module.exports = {
    patients: patients,
    consultations: consultations,
  }

The router where i'm trying to populaet consultation field and then get the item

const {patients} = require('./patient.models')
const {consultations} = require('./patient.models')

// not working , getting empty array
const patient = await patients.find({"consultations.motif" : "Checking"}).populate('consultations')  
                res.send(patient)

The mongo db record , to show you that the field does exist enter image description here

Here's what i get when i do make the following query iwthout specifiying the field

const patient = await patients.find().populate('consultations')
        res.send(patient)

enter image description here

Sb Zakaria
  • 311
  • 6
  • 18

1 Answers1

0

This question already has been answered here: Find after populate mongoose

Here is the solution for your case which does not involve changing the database structure:

const patient = await patients.find().populate({
  path: 'consultations',
  match: {
    motif: 'Checking'
  }
})
res.send(patient)
aweebit
  • 527
  • 3
  • 11
  • it's still not working , i get all records. i'm looking only for the record with checking motif – Sb Zakaria Aug 08 '21 at 11:27
  • There was a typo in my code, maybe that is the reason it did not work if you just copied it. I have just fixed, so please try again. – aweebit Aug 08 '21 at 11:35
  • same.... it's frustrating i have been trying to fix this problem since 2 days..... – Sb Zakaria Aug 08 '21 at 11:41
  • I'm getting all the records once again – Sb Zakaria Aug 08 '21 at 11:42
  • I just realised you're not wrong. What's happening is mongo is sending me all the patient records, the ones that doesn't have that motif will send them with an empty array and still get the patient record thoug h ,got it? – Sb Zakaria Aug 08 '21 at 11:48