Israel kusayev
Israel kusayev

Reputation: 1001

aggregate base on match in the second collection

I'm using mongodb. And I have collection Treatments and collection Patients. I want to find all treatments that their patient.createdBy is equal to the user who asking the data.

So I tried this

 const reminders = await Treatment.aggregate([
    {
      $lookup: {
        from: 'patients',
        localField: 'patientId',
        foreignField: '_id',
        as: 'patient'
      }
    },
    { $project: { reminders: 1, reminderDate: 1 } },
    { $match: { 'patient.createdBy': { $eq: req.user._id } } }
  ]);

According to some examples that i saw, it's should work like this. But it's return me an empty array


If I remove the $match its return me object like this


   {
        "_id": "5d1e64bdc1506a00045c6a6f",
        "date": "2019-07-04T00:00:00.000Z",
        "visitReason": "wewwe",
        "treatmentNumber": 2,
        "referredBy": "wewew",
        "findings": "ewewe",
        "recommendations": "ewew",
        "remarks": "wwewewe",
        "patientId": "5cc9a50fd915120004bf2f4e",
        "__v": 0,
        "patient": [
            {
                "_id": "5cc9a50fd915120004bf2f4e",
                "lastName": "לאון",
                "momName": "רןת",
                "age": "11",
                "phone": "",
                "email": "",
                "createdAt": "2019-05-01T13:54:23.261Z",
                "createdBy": "5cc579d71c9d44000018151f",
                "__v": 0,
                "firstName": "שרה",
                "lastTreatment": "2019-08-02T14:20:08.957Z",
                "lastTreatmentCall": true,
                "lastTreatmentCallDate": "2019-08-04T15:17:35.000Z"
            }
        ]
    }

this is patient schema


const patientSchema = new mongoose.Schema({
  firstName: { type: String, trim: true, required: true },
  lastName: { type: String, trim: true, required: true },
  momName: { type: String, trim: true },
  birthday: { type: Date },
  age: { type: String, trim: true },
  lastAgeUpdate: { type: Date },
  phone: { type: String, trim: true },
  email: { type: String, trim: true },
  createdAt: { type: Date, default: Date.now },
  createdBy: { type: mongoose.Schema.Types.ObjectId, required: true },
  lastTreatment: { type: Date },
  lastTreatmentCall: { type: Boolean },
  lastTreatmentCallDate: { type: Date }
});

And this is treatment schema

const treatmentSchema = new mongoose.Schema({
  date: { type: Date, default: new Date().toISOString().split('T')[0] },
  visitReason: { type: String, trim: true },
  treatmentNumber: { type: Number, required: true },
  referredBy: { type: String, trim: true },
  findings: { type: String, trim: true },
  recommendations: { type: String, trim: true },
  remarks: { type: String, trim: true },
  reminders: { type: String, trim: true },
  reminderDate: { type: Date },
  patientId: { type: mongoose.Schema.Types.ObjectId }
});

what I'm missing

Upvotes: 1

Views: 43

Answers (2)

Ashh
Ashh

Reputation: 46451

You have vanished your patient field in the second last $project stage. So instead use it at the end of the pipeline. Also you need to cast your req.user._id to mongoose objectId

import mongoose from 'mongoose'

const reminders = await Treatment.aggregate([
  {
    $lookup: {
      from: 'patients',
      localField: 'patientId',
      foreignField: '_id',
      as: 'patient'
    }
  },
  { $match: { 'patient.createdBy': { $eq: mongoose.Types.ObjectId(req.user._id) } } },
  { $project: { reminders: 1, reminderDate: 1 } }
])

Upvotes: 2

Binit Ghetiya
Binit Ghetiya

Reputation: 1979

I think you can add using the pipeline, like below

const reminders = await Treatment.aggregate([
{
  $lookup: {
    from: 'patients',
    localField: 'patientId',
    foreignField: '_id',
    as: 'patient',
    pipeline: [{ $match: { 'age': { $eq: "100" } } }]
  }
},

]);

Upvotes: -1

Related Questions